locked
datatable Row and Column ? RRS feed

  • Question

  • I want to update datatable column value according to data-row value like this

    Name    Job_code   FNAME   

    a           101         

    b           105

    c            109

     

    now i want to set value of FNAME Column by using Job_Code Value.

    Tuesday, May 3, 2011 2:29 PM

Answers

  • Loop through the rows of dataTable and do an insert (or update) into "FNAME" column like this:

    for(int i = 0; i < table.Rows.Count;i++)
    {
       table.Rows[i]["FNAME"] = "your value";
    }
    

     

    for example if you have some other 3 different values to insert, you can do it like:

    stirng[] data = new string[]{ "A", "B", "C" };
    for(int i = 0; i < table.Rows.Count;i++)
    {
       table.Rows[i]["FNAME"] = data[i];
    }
    //into 3rd column: 1st row will have A, 2nd row will have B and 3rd row will have C

     

    But you can do your own type of population, this was just an example.

    bye,


    Mitja

    • Marked as answer by Cookie Luo Thursday, May 12, 2011 7:38 AM
    Tuesday, May 3, 2011 2:57 PM
  • foreach(DataRow dr in YourDataTable.Rows)
    {
       dr["FNAME"] = dr["Job_Code"];
    }
    

    Bob - www.crowcoder.com
    • Marked as answer by Cookie Luo Thursday, May 12, 2011 7:38 AM
    Tuesday, May 3, 2011 3:03 PM

All replies

  • Loop through the rows of dataTable and do an insert (or update) into "FNAME" column like this:

    for(int i = 0; i < table.Rows.Count;i++)
    {
       table.Rows[i]["FNAME"] = "your value";
    }
    

     

    for example if you have some other 3 different values to insert, you can do it like:

    stirng[] data = new string[]{ "A", "B", "C" };
    for(int i = 0; i < table.Rows.Count;i++)
    {
       table.Rows[i]["FNAME"] = data[i];
    }
    //into 3rd column: 1st row will have A, 2nd row will have B and 3rd row will have C

     

    But you can do your own type of population, this was just an example.

    bye,


    Mitja

    • Marked as answer by Cookie Luo Thursday, May 12, 2011 7:38 AM
    Tuesday, May 3, 2011 2:57 PM
  • foreach(DataRow dr in YourDataTable.Rows)
    {
       dr["FNAME"] = dr["Job_Code"];
    }
    

    Bob - www.crowcoder.com
    • Marked as answer by Cookie Luo Thursday, May 12, 2011 7:38 AM
    Tuesday, May 3, 2011 3:03 PM