Export to CSV with programmatically generated grid

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

    Posted 10 Aug 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)
    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. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 15 Aug 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";
        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.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. FIG IT
    FIG IT avatar
    2 posts
    Member since:
    Jul 2016

    Posted 15 Aug in reply to Dess Link to this post

    Works ok, thanks
Back to Top