Click here to Skip to main content
15,886,825 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I want to combine two DataTables together and bind it to a single DataGridView such that I can still use the two DataTables separately in ways that I can add, update, or remove rows to either of them. The row changes will be reflected both on tables and on the bind DataGridView as well. How would I do that?
Posted
Comments
syed shanu 5-Nov-14 21:44pm    
Hi,
I ahve a question will you add,update and delete records only in your datatable or to your DB.
if you do in your DB ,Instead of joining 2 tables you can write a new Join query from db and bidn the result to datagridview every time you do insert,update or delete.
if you explain with more details about requirement it will be easy to find the better solution.
Que Trac 5-Nov-14 23:15pm    
Well, I add, update, and delete records both in my local database and in my DataTable. Rather than having to reload all the records from the database constantly, I design it such that all the records from the database is loaded once to DataTable. Then each time a record is added, updated, and removed, the changes will be applied to both without having the need to clear and reload all the records again to optimize performance.

Hi,

You can use Merge table Method

Check This,

C#
private static void DemonstrateMergeTable()
{
    DataTable table1 = new DataTable("Items");

    // Add columns
    DataColumn column1 = new DataColumn("id", typeof(System.Int32));
    DataColumn column2 = new DataColumn("item", typeof(System.Int32));
    table1.Columns.Add(column1);
    table1.Columns.Add(column2);

    // Set the primary key column.
    table1.PrimaryKey = new DataColumn[] { column1 };

  

    // Add some rows.
    DataRow row;
    for (int i = 0; i <= 3; i++)
    {
        row = table1.NewRow();
        row["id"] = i;
        row["item"] = i;
        table1.Rows.Add(row);
    }

    // Accept changes.
    table1.AcceptChanges();
    PrintValues(table1, "Original values");

    // Create a second DataTable identical to the first.
    DataTable table2 = table1.Clone();

    // Add three rows. Note that the id column can't be the  
    // same as existing rows in the original table.
    row = table2.NewRow();
    row["id"] = 14;
    row["item"] = 774;
    table2.Rows.Add(row);

    row = table2.NewRow();
    row["id"] = 12;
    row["item"] = 555;
    table2.Rows.Add(row);

    row = table2.NewRow();
    row["id"] = 13;
    row["item"] = 665;
    table2.Rows.Add(row);

    // Merge table2 into the table1.
    Console.WriteLine("Merging");
    table1.Merge(table2);
    PrintValues(table1, "Merged With table1");

}



Thanks

Siva Rm K
 
Share this answer
 
Comments
Que Trac 5-Nov-14 23:10pm    
I can't use these codes, because I still want the two tables separated.

In case you suggested merging the two together into another table, I don't want to do that as well since I have to re-merge the two tables again every time I add, update, or remove records to any of the two tables.
After searching around online for a solution, I guess there is no easy solution to this problem. DataGridView is designed for handling only one data source. Thus, it isn't possible to combine two DataTable into one and bind that to DataGridView without having to clone or alter the two tables.

Thus, the only solution would be to create another table with the contents of both tables and update that table whenever changes are made to either of the two originating tables.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900