Joining Values Of Two Columns From DataTable
Solution 1
Ok if you really want to do this then you have create a extra DataTable with Three Columns:
TagNumber, LogNumber Combined: as below:
private DataTable CreateDataTableColumns()
{
DataTable dtThreeItems = new DataTable();
dtThreeItems.Columns.Add("TagNumber", typeof(String));
dtThreeItems.Columns.Add("LogNumber", typeof(String));
dtThreeItems.Columns.Add("Combined", typeof(String));
return dtThreeItems;
}
Now iterate the old datatable as below to get combined value:
foreach (DataRow dr in dtTwoItems.Rows)
{
row = dtThreeItems.NewRow();
row["TagNumber"] = dr["TagNumber"].ToString();
row["LogNumber"] = dr["LogNumber"].ToString();
row["Combined"] = dr["TagNumber"].ToString()+"/"+dr["LogNumber"].ToString() ;
dtThreeItems.Rows.Add(row);
}
Thats All
Solution 2
Try doing it like this.
dtTwoItems.Columns.Add("Combined", typeof(string), "TagNumber+'/'+LogNumber");
Solution 3
DataTable is like a container. It is not proper to join tables.
I recommend you'd use linq.
Solution 4
Did you try to Add new Column to DataTable and then iterate through Each Row to put value by combining them?
EDIT: I Am not sure if Linq or Datatable query have some inbuilt feature to do this, but simple solution is what I tell. Or if you are filling your datatable from any SQL Query based database, then write a SQL that has third column with merged value using concat of columns.
Edit2:
foreach (Datarow r in myTable.Rows) {
r["Newcolumn"] = Convert.ToString(r["c1"]) + "/" + Convert.ToString(r["c2"]);
}
Harsh
Updated on April 09, 2020Comments
-
Harsh about 4 years
Joining Values Of Two Columns From DataTable Two columns make it in one columns from datatable
my datatable is
TagNumber, LogNumber Combined 124 1 2 125 1 3 126 2 4 o/p: TagNumber 124 ~1~2 125 ~1~3 126 ~2~4 combined column is merge from column0 and column1
i dont understand hw can i do please write sample of code
I dont have experience on linq . I add column bt hw can i merge two columns in that one columns
I got answer: For i As Integer = 0 To dstemp.Tables(0).Rows.Count - 1 dstemp.Tables(0).Rows(i)(0) = dstemp.Tables(0).Rows(i)("TagNumber") & "~" & dstemp.Tables(0).Rows(i)("LogNumber") & "~" & dstemp.Tables(0).Rows(i)("Combined") next
-
Harsh over 10 yearsI add column bt hw can i merge two columns in that one columns
-
Harsh over 10 yearsI dont have experience on linq
-
Harsh over 10 yearshow can use dattable.merge
-
Harsh over 10 yearsSumit Gupta it gives error Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataRow'.
-
Harsh over 10 yearsguptaji hw can merge it in one column
-
Harsh over 10 yearsguptaji it gives error Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataRow'.
-
Sumit Gupta over 10 yearsNot sure what you want to merge, in loop we already merge it up. It is too simple to explain.
-
Harsh over 10 yearsbut my datatable hv not fixed columns its change according search so hw can merge in existing table
-
Harsh over 10 yearswt simple guptaji try to understand my problem
-
Saturn K over 8 yearsThis is actually the proper way of doing it. Iterating through each row is less efficient and maintainable.