locked
Merge Multiple DataTables and Bind into a Single DataGridView RRS feed

  • Question

  • Hello All,

    Good Evening..

    I have a situation where, I need to merge mutliple Datatables into one datable and bind it in the gridview.

    Example: GetScheduledTasks()

           DataTable items = new DataTable();
             items.Columns.Add("Name");
             items.Columns.Add("State/Value");
             items.Columns.Add("LastRunStatus");
             items.Columns.Add("StartUpType");
             items.Columns.Add("Type");
             items.Columns.Add("StateError");
             items.Columns.Add("RunError");

    This is one datable in one method. I have 5 methods called by checkbox option individually which performs its own functions and writes into the data table.


    My other Methods are, GetRegistryValues(), GetAppPools(), GetServices() etc., Each method is called by the check box action and binds the datatable into DataGridView.

    When I need to call all the methods  at once or more than one method at a time by "Select All" checkbox, Now obviously the datagridview will be overwrriten by consecutive method's binding and only the last datatable's value from the last called method will be displayed..

    How Can I consolidate all these Datatables and hold it in a consolidated Datatable and then bind finally. The final DataGridView output should have the data from all the methods.

    Note: Each Datatable exist inside its own method. All the Datatables does have same number of columns. Just the data varies.

    I did find this link useful, but little bit confusing.. 

    https://social.msdn.microsoft.com/Forums/windows/en-US/ea0276d6-ab45-49a0-b7d8-d75576576a61/merge-multiple-datatables-in-c?forum=netfx64bit


    - Neuronring




    • Edited by neuronring Thursday, October 9, 2014 10:28 PM
    Thursday, October 9, 2014 10:23 PM

Answers

  • You can create a global DataTable, and in each method you can create an instance of it and put the result to each method's datatable (or the global Datatable) and then you can bind the Global datatable to a gridview.
    • Marked as answer by neuronring Friday, October 10, 2014 2:13 PM
    Friday, October 10, 2014 3:59 AM
  • To add to @qhdoanh's reply, you use the DataTable.Merge() method to merge your DataTables. So, say that your global DataTable is called dtGlobal. You initially don't even have to give it any columns. Your Merge() will do that. So, for example, in your GetScheduledTasks() method, you'd do something like this:

             DataTable items = new DataTable();
             items.Columns.Add("Name");
             items.Columns.Add("State/Value");
             items.Columns.Add("LastRunStatus");
             items.Columns.Add("StartUpType");
             items.Columns.Add("Type");
             items.Columns.Add("StateError");
             items.Columns.Add("RunError");
    
             // add or get the data
             // ........
    
             // then merge with global
             dtGlobal.Merge(items);
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by neuronring Friday, October 10, 2014 2:13 PM
    Friday, October 10, 2014 5:10 AM

All replies

  • You can create a global DataTable, and in each method you can create an instance of it and put the result to each method's datatable (or the global Datatable) and then you can bind the Global datatable to a gridview.
    • Marked as answer by neuronring Friday, October 10, 2014 2:13 PM
    Friday, October 10, 2014 3:59 AM
  • To add to @qhdoanh's reply, you use the DataTable.Merge() method to merge your DataTables. So, say that your global DataTable is called dtGlobal. You initially don't even have to give it any columns. Your Merge() will do that. So, for example, in your GetScheduledTasks() method, you'd do something like this:

             DataTable items = new DataTable();
             items.Columns.Add("Name");
             items.Columns.Add("State/Value");
             items.Columns.Add("LastRunStatus");
             items.Columns.Add("StartUpType");
             items.Columns.Add("Type");
             items.Columns.Add("StateError");
             items.Columns.Add("RunError");
    
             // add or get the data
             // ........
    
             // then merge with global
             dtGlobal.Merge(items);
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by neuronring Friday, October 10, 2014 2:13 PM
    Friday, October 10, 2014 5:10 AM
  • You Guys are Awesome !! Thanks So much !!

    - Neuronring

    Friday, October 10, 2014 2:13 PM
  • You're welcome! Glad we could help! =0)

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Friday, October 10, 2014 3:22 PM