Bind RadGridView to DataTable programatically

2 posts, 0 answers
  1. Greg
    Greg avatar
    8 posts
    Member since:
    Jan 2009

    Posted 23 Sep 2016 Link to this post



    I'm trying to use RadGridView to export a datatable to Excel.  I've created a method that takes a datatable as a parameter, creates a RadGridView, sets the dataSource to the datatable, and then exports.  Problem is that the RadGridview doesn't seem to be getting populated with the contents of the dataTable.  I feel like I'm missing something like 'gridView.DataBind();' but I can't find anything like that in the documentation.  Code is below:

    public static void ExportToExcel(DataTable myDT)

            {             using (Telerik.WinControls.UI.RadGridView grid = new Telerik.WinControls.UI.RadGridView())


                    grid.AutoGenerateColumns = true;

                    grid.DataSource = myDT;



                    GridViewSpreadExport spreadExporter = new GridViewSpreadExport(grid);                 SpreadExportRenderer exportRenderer = new SpreadExportRenderer();

                    spreadExporter.RunExport("c:\\exportedFile.xlsx", exportRenderer);



  2. Dess | Tech Support Engineer, Sr.
    Dess | Tech Support Engineer, Sr.  avatar
    4077 posts

    Posted 27 Sep 2016 Link to this post

    Hello Greg,

    Thank you for writing.  

    The provided code snippet is greatly appreciated. Note that it is important to initialize the RadGridView.BindingContext before setting the DataSource property:

    public Form1()
        DataTable dt = new DataTable();
        for (int i = 0; i < 10; i++)
            dt.Rows.Add(i, "Item" + i);
        using (Telerik.WinControls.UI.RadGridView grid = new Telerik.WinControls.UI.RadGridView())
            grid.AutoGenerateColumns = true;
            grid.BindingContext = new System.Windows.Forms.BindingContext();
            grid.DataSource = dt;
            GridViewSpreadExport spreadExporter = new GridViewSpreadExport(grid);                
            SpreadExportRenderer exportRenderer = new SpreadExportRenderer();
            string fileName = @"..\..\exported" + DateTime.Now.ToLongTimeString().Replace(":", "_") + ".xlsx";
            spreadExporter.RunExport(fileName, exportRenderer);

    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.
Back to Top