How do I change specific data in a data column?

I have populated a DataTable using a DataAdapter, but I would like to dynamically change the values ​​for all rows in that column in the DataTable. How should I do it?

Here is my current code:

SqlConnection conn = null;
string sSQL = "";
string connString = "Datasourceetc";

sSQL = "SELECT TEST_ID FROM TEST_TABLE";

SqlDataAdapter sda = new SqlDataAdapter(sSQL, conn);

DataTable dt = new DataTable();

sda.Fill(dt);

foreach (DataRow row in dt.Rows)
{
  dt.Columns["TEST_ID"] = "changed"; //Not changing it?!
}

GridView1.DataSource = dt;

GridView1.DataBind();

      

thank

+2


source to share


2 answers


All of the above looks good, except where iterating through the rows allows you to access columns like this ...



foreach (DataRow row in dt.Rows)
{
  row["ColumnName"] = "changed"; //Not changing it?!
}

      

+3


source


First you need to fill in and then change!

SqlConnection conn = new SqlConnection(connString);
string sSQL = "SELECT TEST_ID FROM TEST_TABLE";

DataTable dt = new DataTable();

SqlDataAdapter sda = new SqlDataAdapter(sSQL, conn);

// fill the DataTable - now you should have rows and columns!
sda.Fill(dt);

// one you've filled your DataTable, you should be able
// to iterate over it and change values
foreach (DataRow row in dt.Rows)
{
    row["TEST_ID"] = "changed"; //Not changing it?!
}

GridView1.DataSource = dt;
GridView1.DataBind();

      



Mark

0


source







All Articles