1

I am new to C# so i dont know if this is easy. I have two Datatables Table1

abc.Field2 abc.dfg.Field3 abc.Field1
Value2 Value3 Value1
Value2 Value3 Value1
Value2 Value3 Value1
Value2 Value3 Value1

Table2

Field1 Field2 Field3

I need to check by column name in two tables and add value in correct column in table2

like Table2 after

Field1 Field2 Field3
Value1 Value2 Value3
Value1 Value2 Value3
Value1 Value2 Value3
Value1 Value2 Value3

Take care i need a generic solution because there several tables to check and update i cant go with something like column.columnname["Field1"] etc...

Can anyone Help?

john341
  • 13
  • 4

1 Answers1

1

Just rename the existing columns :

            DataTable dt = new DataTable();
            dt.Columns.Add("abc.Field2", typeof(string));
            dt.Columns.Add("abc.Field3", typeof(string));
            dt.Columns.Add("abc.Field1", typeof(string));

            dt.Rows.Add(new object[] {"Value2", "Value3", "Value1"});
            dt.Rows.Add(new object[] { "Value2", "Value3", "Value1" });
            dt.Rows.Add(new object[] { "Value2", "Value3", "Value1" });
            dt.Rows.Add(new object[] { "Value2", "Value3", "Value1" });

            foreach (DataColumn col in dt.Columns.Cast<DataColumn>())
            {
                int index = col.ColumnName.IndexOf(".");
                col.ColumnName = col.ColumnName.Substring(index + 1);
            }

 
        }
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Thank you very much with your solution and ImportRow after that i solved my problem...Thank you again all for the help... – john341 Mar 16 '23 at 14:48