How to copy the value of one data column to another data column in the same c # data table?

I want to copy itemarray[4] from datatable to itemarray[6] this datatable. I used this code and I did not see any changes:

 foreach (DataRow dr_row in dt_table.Rows) { foreach (var field_value in dr_row.ItemArray) { object cell_data = field_value; if (dr_row.ItemArray[6].ToString() == "") { dr_row.ItemArray[6] = dr_row.ItemArray[4]; } original_data += cell_data.ToString(); } original_data += Environment.NewLine; } 
+4
source share
2 answers

First of all, never do this:

 dr_row.ItemArray[6].ToString() == "" 

Change it like this:

 dr_row.ItemArray[6].ToString() == String.Empty 

or

  String.IsNullOrEmpty(dr_row.ItemArray[6].ToString()) 

However, this is just good practice. Now, to the problem you are facing.

What Itemarray does Itemarray , it creates a new array from the string, so if you change the array, you will not change the string.

Do it:

 dr_row[6] = dr_row[4]; 

Must work.

+2
source

Try it,

  foreach (DataRow dr_row in dt_table.Rows) { dr_row[6] = dr_row[4]; } 

and use System.Text.StringBuilder to add data.

  System.Text.StringBuilder sb = new StringBuilder(); sb.Append(value1); 
+1
source

All Articles