最近在做一个项目时,有一个要求是选择GRIDVIEW列表中的项(通过CHECKBOX),然后删除所选择的数据
代码如下:
/// <summary>
/// 摘要:删除单据明细列表中选择的明细信息
/// 创建人:贺振军
/// 创建日期:2009.07.09
/// </summary>
private void DelDjMx()
{
foreach (GridViewRow row in this.gvordermx.Rows)
{
CheckBox chk = (CheckBox)row.Cells[0].FindControl("chkrow");
if (chk.Checked == true)
{
//删除临时表中的数据
string strpk = row.Cells[1].Text
ordermx.Table_Dele_Temp(strpk);
}
}
this.gvordermx.DataSource = ordermx.TempTabl;
this.gvordermx.DataBind();
}
在运行时发现因为第二列我做成隐藏列,所以使用 row.Cells[1].Text 永远取到的都是空值(原来VS2003可能可以取到,但VS2008,VS2005不能取),经过测试可以将这一列设置为关键字列即 DataKey = "XH",然后将此列隐藏即可,列值就可以通过下面的代码即可取得。
/// <summary>
/// 摘要:删除单据明细列表中选择的明细信息
/// 创建人:贺振军
/// 创建日期:2009.07.09
/// </summary>
private void DelDjMx()
{
foreach (GridViewRow row in this.gvordermx.Rows)
{
CheckBox chk = (CheckBox)row.Cells[0].FindControl("chkrow");
if (chk.Checked == true)
{
//删除临时表中的数据
string strpk = gvordermx.DataKeys[row.RowIndex].Value.ToString();
ordermx.Table_Dele_Temp(strpk);
}
}
this.gvordermx.DataSource = ordermx.TempTabl;
this.gvordermx.DataBind();
}
特此记录一下