Joining Values Of Two Columns From DataTable

24,772

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"]);
}
Share:
24,772
Harsh
Author by

Harsh

Updated on April 09, 2020

Comments

  • Harsh
    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
    Harsh over 10 years
    I add column bt hw can i merge two columns in that one columns
  • Harsh
    Harsh over 10 years
    I dont have experience on linq
  • Harsh
    Harsh over 10 years
    how can use dattable.merge
  • Harsh
    Harsh over 10 years
    Sumit Gupta it gives error Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataRow'.
  • Harsh
    Harsh over 10 years
    guptaji hw can merge it in one column
  • Harsh
    Harsh over 10 years
    guptaji it gives error Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataRow'.
  • Sumit Gupta
    Sumit Gupta over 10 years
    Not sure what you want to merge, in loop we already merge it up. It is too simple to explain.
  • Harsh
    Harsh over 10 years
    but my datatable hv not fixed columns its change according search so hw can merge in existing table
  • Harsh
    Harsh over 10 years
    wt simple guptaji try to understand my problem
  • Saturn K
    Saturn K over 8 years
    This is actually the proper way of doing it. Iterating through each row is less efficient and maintainable.