我已经使用DataAdapter填充了DataTable,但我想为DataTable中该列中的所有行动态更改值。我该怎么做呢?
这是我目前的代码:
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();
由于
答案 0 :(得分:3)
上面的所有内容看起来都不错,除非在遍历行时您可以像这样访问列...
foreach (DataRow row in dt.Rows)
{
row["ColumnName"] = "changed"; //Not changing it?!
}
答案 1 :(得分:0)
您需要先填写,然后再进行更改!
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();
马克