This is a migrated thread and some comments may be shown as answers.

Export to CSV with programmatically generated grid

2 Answers 117 Views
GridView
This is a migrated thread and some comments may be shown as answers.
DBE
Top achievements
Rank 1
DBE asked on 10 Aug 2016, 04:47 PM

I'm trying to use the ExportToCSV function and I can see it works ok with one of my grids, one that was created with the Designer. but when trying to generate a GridView on the fly it'll export a blank CSV, even when the grid's DataSource is filled with records.

01.private void radButton1_Click(object sender, EventArgs e)
02.{
03.    RadGridView tempGrid = new RadGridView();
04. 
05.    GridViewTextBoxColumn firstName = new GridViewTextBoxColumn();
06.    firstName.FieldName = "fname";
07.    tempGrid.Columns.Add(firstName);
08. 
09.    GridViewTextBoxColumn lastName = new GridViewTextBoxColumn();
10.    lastName.FieldName = "lname";
11.    tempGrid.Columns.Add(lastName);
12. 
13.    GridViewTextBoxColumn emailAddress = new GridViewTextBoxColumn();
14.    emailAddress.FieldName = "email";
15.    tempGrid.Columns.Add(emailAddress);
16. 
17.    ContactsEntities csvList = new ContactsEntities();
18.    tempGrid.DataSource = (from c in csvList.CSVList select c).ToList();
19. 
20.    ExportToCSV exporter = new ExportToCSV(tempGrid);
21.    string fileName = "D:\\ExportedData2.csv";
22.    exporter.RunExport(fileName);
23.}

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 15 Aug 2016, 06:28 AM
Hello ,

Thank you for writing. 

When you add the grid at run time, note that it is necessary to initialize its BindingContext before populating with data in order to export the grid content:
private void radButton1_Click(object sender, EventArgs e)
{
    RadGridView tempGrid = new RadGridView();
    tempGrid.BindingContext = new System.Windows.Forms.BindingContext();
    GridViewTextBoxColumn firstName = new GridViewTextBoxColumn();
    firstName.FieldName = "fname";
    tempGrid.Columns.Add(firstName);
 
    GridViewTextBoxColumn lastName = new GridViewTextBoxColumn();
    lastName.FieldName = "lname";
    tempGrid.Columns.Add(lastName);
 
    GridViewTextBoxColumn emailAddress = new GridViewTextBoxColumn();
    emailAddress.FieldName = "email";
    tempGrid.Columns.Add(emailAddress);
 
    tempGrid.DataSource = GetData();
 
    ExportToCSV exporter = new ExportToCSV(tempGrid);
    string fileName = @"..\..\" + DateTime.Now.ToLongTimeString().Replace(":", "_") + ".csv";
    exporter.RunExport(fileName);
}
 
private object GetData()
{
    DataTable dt = new DataTable();
    dt.Columns.Add("Id");
    dt.Columns.Add("Name");
    for (int i = 0; i < 5; i++)
    {
        dt.Rows.Add(i, "Item" + i);
    }
    return dt;
}

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Telerik by Progress
Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
0
DBE
Top achievements
Rank 1
answered on 15 Aug 2016, 11:05 AM
Works ok, thanks
Tags
GridView
Asked by
DBE
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
DBE
Top achievements
Rank 1
Share this question
or