Export to CSV with programmatically generated grid

3 posts, 0 answers
  1. FIG IT
    FIG IT avatar
    4 posts
    Member since:
    Jul 2016

    Posted 10 Aug 2016 Link to this post

    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)
    03.    RadGridView tempGrid = new RadGridView();
    05.    GridViewTextBoxColumn firstName = new GridViewTextBoxColumn();
    06.    firstName.FieldName = "fname";
    07.    tempGrid.Columns.Add(firstName);
    09.    GridViewTextBoxColumn lastName = new GridViewTextBoxColumn();
    10.    lastName.FieldName = "lname";
    11.    tempGrid.Columns.Add(lastName);
    13.    GridViewTextBoxColumn emailAddress = new GridViewTextBoxColumn();
    14.    emailAddress.FieldName = "email";
    15.    tempGrid.Columns.Add(emailAddress);
    17.    ContactsEntities csvList = new ContactsEntities();
    18.    tempGrid.DataSource = (from c in csvList.CSVList select c).ToList();
    20.    ExportToCSV exporter = new ExportToCSV(tempGrid);
    21.    string fileName = "D:\\ExportedData2.csv";
    22.    exporter.RunExport(fileName);

  2. Dess
    Dess avatar
    2164 posts

    Posted 15 Aug 2016 Link to this post

    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";
        GridViewTextBoxColumn lastName = new GridViewTextBoxColumn();
        lastName.FieldName = "lname";
        GridViewTextBoxColumn emailAddress = new GridViewTextBoxColumn();
        emailAddress.FieldName = "email";
        tempGrid.DataSource = GetData();
        ExportToCSV exporter = new ExportToCSV(tempGrid);
        string fileName = @"..\..\" + DateTime.Now.ToLongTimeString().Replace(":", "_") + ".csv";
    private object GetData()
        DataTable dt = new DataTable();
        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.

    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.
  3. FIG IT
    FIG IT avatar
    4 posts
    Member since:
    Jul 2016

    Posted 15 Aug 2016 in reply to Dess Link to this post

    Works ok, thanks
Back to Top