Thursday, May 08, 2025

Breaking News
>> Securing and encrypt View State and Cookies values  >> Page has one or more controls that do not correspond with   >> “The Controls collection cannot be modified because the control contains code blocks”  >> How to fix "Validation(): Element 'xxxx' is not supported  >> How to create a new session in ASP.NET programmatically  >> MySQL Database Backup using mysqldump command    

Editors Picks

Friday, March 11, 2011

Merging Two DataTables in C#

 Merging Two DataTables in C#

private void MergeDataTable()
        {

            DataTable dtOne = new DataTable("FirstTable");

            dtOne.Columns.Add("MyColumn");

            DataTable dtTwo = new DataTable("SecondTable");

            dtTwo.Columns.Add("MyColumn");

            for (int i = 1; i <= 10; i++)
            {

                DataRow row = dtOne.NewRow();

                row["MyColumn"] = "Data" + i;

                dtOne.Rows.Add(row);

            }

            for (int i = 11; i <= 20; i++)
            {

                DataRow row = dtTwo.NewRow();

                row["MyColumn"] = "Data" + i;

                dtTwo.Rows.Add(row);

            }

            DataSet ds = new DataSet();

            ds.Tables.Add(dtOne);

            ds.Merge(dtTwo);

            DataGrid1.DataSource = ds;

            DataGrid1.DataBind();

        }

No comments :

Post a Comment

Contact Us

Name

Email *

Message *