我正在使用ASP.net项目,并且有一个gridview,它有一个来自sql数据库的数据集作为数据源。
当我更改gridview中的值时,我想更新数据集,以便我可以使用该数据集再次更新数据库。
我到目前为止的陈述。
这是我遇到麻烦的部分。我可以获取所选行,但不能使用所选列名来更新数据集。
myDataSet.Tables[0].Rows[e.RowIndex][?] = "";
RowUpdating事件的完整代码。
protected void grdViewDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Just get the dataset populated from my database (sql)
DataSet dsOriginal = dbConn.returnSqlDataset(query);
//update the dataset here so that you can update the database again.
foreach (TableCell cell in grdViewDetails.Rows[e.RowIndex].Cells)
{
//set the employeeid so you can update the dataset
if (cell.Controls[0] is TextBox)
{
TextBox textbox = (TextBox)cell.Controls[0];
string value = textbox.Text; //This is just a tester to see if the value is correct
// dsOriginal.Tables[0].Rows[e.RowIndex][?] = value; //Here is the problem, how to get the selected column name
}
else
{
if (cell.Controls[0] is CheckBox)
{
CheckBox chkBoxWeek = (CheckBox)cell.Controls[0];
Boolean checkStatus = chkBoxWeek.Checked; //This is just a tester to see if the value is correct
//dsOriginal.Tables[0].Rows[e.RowIndex][?] = checkStatus; //Here is the problem, how to get the selected column name
}
}
}
//Use the updated dataset to update the database with.
dbConn.udpatCourse(dsOriginal );
}
答案 0 :(得分:1)
您可以遍历GridViewUpdateEventArgs.NewValues
集合以查找已更新的内容。
这稍微改编自链接的MSDN文档:
foreach (DictionaryEntry entry in e.NewValues)
{
string columName = entry.Key;
string userInput = e.NewValues[entry.Key];
// Now you can do stuff with this info that meets your logic needs
}
如果我正确地阅读了你的问题,你应该能够从这里得到你需要的东西。