How to update a value in a column in a datatable w

2019-04-13 16:42发布

I want to update all columns one by one in a datatable using a foreach loop. The code below is what I have so far. But it does not seem to work. Your help will be much appreciated.

 foreach (DataRow row in myTable.Rows) 
 {
     Double i;
     Double j = Convert.ToDouble(row["x"]);
     int y = 1;

     int aan = (int)row["year"];

         if(y == aan) 
         {
            i = j + 2;
         }

     row["x"]=i;
     row.EndEdit();
     myTable.AcceptChanges();

  }

1条回答
霸刀☆藐视天下
2楼-- · 2019-04-13 17:15

The code works fine for me, except for a few tweaks. The code is given below:

        foreach (DataRow row in myTable.Rows) 
        {
             Double i = 0;
             Double j = Convert.ToDouble(row["x"]);
             int y = 1;

             int aan = Convert.ToInt32(row["year"]);

                 if(y == aan) 
                 {
                    i = j + 2;
                 }

             row["x"]=i;
             row.EndEdit();
             myTable.AcceptChanges();

        }

Are you facing any specific issues?

查看更多
登录 后发表回答