Friday, 24 May 2013

how to copy data from a datatable to another datatable in C#.net

 DataTable dt = new DataTable();
                dt.Columns.Add("EID", typeof(string));
                dt.Columns.Add("NAME", typeof(string));
                DataRow dr;
                dr = dt.NewRow();
                dr[0] = "1";
                dr[1] = "siv";
                dt.Rows.Add(dr);
                DataRow dr1;
                dr1 = dt.NewRow();
                dr1[0] = "2";
                dr1[1] = "sankar";
                dt.Rows.Add(dr1);
                DataTable dt1 = dt.Clone();
                IEnumerable<DataRow> idr = (from x in dt.AsEnumerable()  select x).ToArray();
                idr.CopyToDataTable(dt1, LoadOption.PreserveChanges);
                dataGridView1.DataSource = dt1;
Other process
 DataTable dt = new DataTable();
                dt.Columns.Add("EID", typeof(string));
                dt.Columns.Add("NAME", typeof(string));
                DataRow dr;
                dr = dt.NewRow();
                dr[0] = "1";
                dr[1] = "siv";
                dt.Rows.Add(dr);
                DataRow dr1;
                dr1 = dt.NewRow();
                dr1[0] = "2";
                dr1[1] = "sankar";
                dt.Rows.Add(dr1);
                DataTable dt1 = dt.Clone();
                List<DataRow> lst = dt.AsEnumerable().ToList();
                lst.CopyToDataTable(dt1, LoadOption.PreserveChanges);
                dataGridView1.DataSource = dt1;
Other Process
DataTable dt = new DataTable();
                dt.Columns.Add("EID", typeof(string));
                dt.Columns.Add("NAME", typeof(string));
                DataRow dr;
                dr = dt.NewRow();
                dr[0] = "1";
                dr[1] = "siv";
                dt.Rows.Add(dr);
                DataRow dr1;
                dr1 = dt.NewRow();
                dr1[0] = "2";
                dr1[1] = "sankar";
                dt.Rows.Add(dr1);
                DataTable dt1 = dt.Clone();
                IEnumerable<DataRow> lst = dt.AsEnumerable();
                lst.CopyToDataTable(dt1, LoadOption.PreserveChanges);
                dataGridView1.DataSource = dt1;

No comments:

Post a Comment