Sponsored Ad

Sunday, March 14, 2010

How to Copy DataRows from another DataTable

 

In this example, oDs is the original dataset which have datatable inside and multiple rows are there. Now this example demonstrate only copying 3 rows you can set as the counter as per your requirement. In this code first create a datatable using the same structure of table from where you have to copy a row, otherwise create same structure manually. to copy only table structure use .clone method

DataTable dt = oDs.Tables[0].Clone();

and then inside the for loop use importrow() method to copy the exact row.

dt.ImportRow(oDs.Tables[0].Rows[i]);

 //Copy DataRows from another table

        DataSet oDs = getData();
        DataTable dt = oDs.Tables[0].Clone();
        if (oDs != null && oDs.Tables.Count > 0 && oDs.Tables[0].Rows.Count > 2)
        {
            for (int i = 1; i <= 3; i++)
            {
                dt.ImportRow(oDs.Tables[0].Rows[i]);
            }
        }

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers