ExportToCSV - Extra row after last row of data

1 Answer 51 Views
GridView
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
Mark asked on 06 Feb 2023, 06:57 PM

There seems to be an extra row created, w/ no data after the last row of data is added to the CSV.   Not sure what is happening or if there is a setting to not include the last empty row.  Just so we are clear, I have a grid with 10 rows in it, but when I look at the csv that is generated from the grid, I have 11 rows.   

 

 

Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
commented on 06 Feb 2023, 07:21 PM

As you can see by the attached screen shot, there is a 4th row, 1st row is the column headers and the 2nd and 3rd are data.

 

Here is my sample code

 


public class MyData
{
	public string C1 { get; set; }
	public string C2 { get; set; }
}
void Main()
{
	List<MyData> list = new List<MyData>();
	list.Add(new MyData { C1 = "L1C1", C2 = "L1C2"});
	list.Add(new MyData { C1 = "L2C1", C2 = "L2C2"});
	
	
	RadGridView grid = new RadGridView();
	grid.BeginUpdate();
	grid.AutoGenerateColumns = true;
	grid.AutoGenerateHierarchy = true;
	grid.BindingContext = new BindingContext();
	grid.DataSource = list;
	grid.ShowColumnHeaders = true;
	grid.EndUpdate();
	
	ExportToCSV exporter = new ExportToCSV(grid);
	
	exporter.FileExtension = "CSV";
	exporter.RunExport($@"C:\Temp\Temp.csv");

	
}

1 Answer, 1 is accepted

Sort by
0
Todor
Telerik team
answered on 07 Feb 2023, 03:57 PM

Hello, Mark,

Thank you for the sample code and illustration.

I have tested and reproduced the problem. I can confirm this is not a desired behavior. This is why I have logged a bug report in our Feedback portal by creating a public thread on your behalf. You can track its progress, subscribe for status changes and add your comments on the following link - RadGridView: ExportToCSV adds an empty row after the last row with data.

Find your Telerik points updated as a small sign of gratitude for bringing this issue to our attention.

As a workaround, I can suggest you use the following approach: After the export open the CSV text file, remove the new line and save the updated text back to the file:

private void radButton1_Click(object sender, EventArgs e)
{
    ExportToCSV exporter = new ExportToCSV(this.radGridView1);
    exporter.RunExport(@"C:\Temp\Temp.csv");

    // Open the CSV text file and remove the new line.
    string text = File.ReadAllText(@"C:\Temp\Temp.csv");
    // Text ends with Environment.NewLine, so we need to trim it.
    text = text.Substring(0, text.Length - Environment.NewLine.Length);
    File.WriteAllText(@"C:\Temp\Temp.csv", text);
}

I hope this information helps. If you need any further assistance please don't hesitate to contact me.  

Regards,
Todor Vyagov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
GridView
Asked by
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
Answers by
Todor
Telerik team
Share this question
or