一个取值问题,谢谢!
做个一全选删除功能,由button1激发,遇到取值主键问题~`!
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox chk = (CheckBox)gr.Cells[0].FindControl( "itemchk ");
if (chk.Checked)
{
string id = GridView1.DataKeys.ToString(); //想获取当前行ID值,即主键值。
SqlDataSource1.UpdateCommand = "UPDATE [ProjectData] SET [Passed] = 1 WHERE [ID] = ' " + id + " ' ";
SqlDataSource1.Update();
}
}
}
提示错误,值不正确,update失败!
------解决方案--------------------> >
foreach (GridViewRow gr in GridView1.Rows)
{
// bad method
// CheckBox chk = (CheckBox)gr.Cells[0].FindControl( "itemchk ");
// just as below
CheckBox chk = (CheckBox)gr.FindControl( "itemchk ");
if (chk.Checked)
{
//string id = GridView1.DataKeys.ToString(); //想获取当前行ID值,即主键值。
//string id = GridView1.DataKeys[gr.RowIndex].Value.ToString(); //想获取当前行ID值,即主键值。
SqlDataSource1.UpdateCommand = "UPDATE [ProjectData] SET [Passed] = 1 WHERE [ID] = ' " + id + " ' ";
SqlDataSource1.Update();
}