Binding 2 data tables to a single data grid
-
How to bind two data tables which are in the same dataset to a datagrid????Below is tha code i've written so far OleDbDataAdapter da_pay = new OleDbDataAdapter(); DataSet ds_pay = new DataSet(); OleDbCommand pay_cmd = new OleDbCommand("select Group from group_details where [Group]='"+descriptiondpdwnlist .SelectedItem .Value +"'",con2); da_pay.SelectCommand = pay_cmd; da_pay.Fill(ds_pay,"bill_group"); DataTable pay_dt=ds_pay .Tables ["bill_group"]; OleDbCommand pay_cmd1 = new OleDbCommand("select Description from description_details where [Description]='"+descrptndpdnlt .SelectedItem .Text +"'",con2); da_pay.SelectCommand = pay_cmd1; da_pay.Fill(ds_pay ,"Bill_description"); DataTable pay_dt1=ds_pay .Tables ["Bill_description"]; con2.Close();
-
How to bind two data tables which are in the same dataset to a datagrid????Below is tha code i've written so far OleDbDataAdapter da_pay = new OleDbDataAdapter(); DataSet ds_pay = new DataSet(); OleDbCommand pay_cmd = new OleDbCommand("select Group from group_details where [Group]='"+descriptiondpdwnlist .SelectedItem .Value +"'",con2); da_pay.SelectCommand = pay_cmd; da_pay.Fill(ds_pay,"bill_group"); DataTable pay_dt=ds_pay .Tables ["bill_group"]; OleDbCommand pay_cmd1 = new OleDbCommand("select Description from description_details where [Description]='"+descrptndpdnlt .SelectedItem .Text +"'",con2); da_pay.SelectCommand = pay_cmd1; da_pay.Fill(ds_pay ,"Bill_description"); DataTable pay_dt1=ds_pay .Tables ["Bill_description"]; con2.Close();
public void GridBind() { DataTable dtfirst;//your first datatable DataTable dtsecond;//your second datatable DataTable dtOverall = new DataTable(); dtOverall.Columns.Add("id");//name of the first table column dtOverall.Columns.Add("name");//name of the first table column dtOverall.Columns.Add("email");//name of the second table column dtOverall.Columns.Add("phone");//name of the second table column //.. //.. //.. int first = dtfirst.Rows.Count; int second = dtsecond.Rows.Count; int overAllTableCount = (first > second) ? first : second; for (int i = 0; i < overAllTableCount; i++) { int id = 0;//according to the column name and datatype use here string name = string.Empty; string email = string.Empty; string phone = 0; //... //.. //.. if (i < first) { id = int.Parse(dtfirst.Rows[i]["id"].ToString()); name = dtfirst.Rows[i]["name"].ToString(); } if (i < second) { email = second.Rows[i]["email"].ToString(); phone = int.Parse(second.Rows[i]["phone"].ToString()); } DataRow drOverall = dtOverall.NewRow(); drOverall["id"] = id; drOverall["name"] = name; drOverall["email"] = email; drOverall["phone"] = phone; dtOverall.Rows.Add(drOverall); } } //hope you understood
Padmanabhan My Articles: Articles[^] My latest Article: Word Automation[^]