Click here to Skip to main content
15,886,791 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi every one im trying to change the value of a row in an datatable in an foreach loop

C#
protected void btnFixZero(object sender, EventArgs e)
{
    System.Data.DataTable dt = new System.Data.DataTable();
    dt = (System.Data.DataTable)ViewState["SelectedRecords"];
    int rowscount = GridView2.Rows.Count;
    int columnscount = GridView2.Columns.Count;
    if (missingCell == 0)
    {
        foreach (DataRow row in dt.Rows)
        {

            string cellData = row["F4"].ToString(); //original value
            string sub0 = cellData.Substring(0, 1);
            string sub1 = cellData.Substring(1, 1);
            string subplus = cellData.Substring(0, 1);

            if (sub0 == "2" && sub1 == "7")
            {
                cellData = cellData.Remove(0, 2);
                cellData = "0" + cellData;//change original value save it   back to the datatable
                row.AcceptChanges();
                dt.AcceptChanges();
            }
            if (sub0 != "0")
            {
                cellData = "0" + cellData;//change original value save it   back to the datatable
                row.AcceptChanges();
                dt.AcceptChanges();
            }

        }
        ViewState["SelectedRecords"] = dt;


        GridView2.DataSource = dt;
        GridView2.DataBind();
    }

       }
Posted
Comments
[no name] 9-Jan-13 5:19am    
what's the problem now...
mrDivan 9-Jan-13 5:28am    
hallo thank you for your response the problem is that it does not save the updated value back to dt//the datatable

1 solution

C#
protected void btnFixZero(object sender, EventArgs e)
      {
          System.Data.DataTable dt = new System.Data.DataTable();
          dt = (System.Data.DataTable)ViewState["SelectedRecords"];
          int rowscount = GridView2.Rows.Count;
          int columnscount = GridView2.Columns.Count;
          if (missingCell == 0)
          {
              foreach (DataRow row in dt.Rows)
              {

                  string cellData = row["F4"].ToString();
                  string sub0 = cellData.Substring(0, 1);
                  string sub1 = cellData.Substring(1, 1);
                  string subplus = cellData.Substring(0, 1);

                  if (sub0 == "2" && sub1 == "7")
                  {
                      cellData = cellData.Remove(0, 2);
                      cellData = "0" + cellData;
                      row["F4"] = cellData;//had to specify which column
                      row.AcceptChanges();
                      dt.AcceptChanges();
                  }
                  if (sub0 != "0")
                  {
                      cellData = "0" + cellData;
                      row["F4"] = cellData;//had to specify which column
                      row.AcceptChanges();
                      dt.AcceptChanges();
                  }

              }
              dt.AcceptChanges();
              ViewState["SelectedRecords"] = dt;


              GridView2.DataSource = dt;
              GridView2.DataBind();
          }

             }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900