嗨gusy我试图理解如何使用带有复选框的grindview从数据库中删除但我不知道为什么deleteresults.text和DeleteResults.Visible不工作这是我的代码(我在编程中的新:)和thx为你的答案
protected void Button3_Click(object sender, EventArgs e)
{
bool atLeastOneRowDeleted = false;
// Iterate through the Products.Rows property
foreach (GridViewRow row in Products.Rows)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("selector");
if (cb != null && cb.Checked)
{
// Delete row! (Well, not really...)
atLeastOneRowDeleted = true;
// First, get the ProductID for the selected row
int id_offre = Convert.ToInt32(Products.DataKeys[row.RowIndex].Value);
// "Delete" the row
DeleteResults.Text += string.Format("This would have deleted ProductID {0}<br />", id_offre);
}
}
// Show the Label if at least one row was deleted...
DeleteResults.Visible = atLeastOneRowDeleted;
}
答案 0 :(得分:0)
您是否在DataKeyNames
上设置了GridView
属性?您需要这样做才能使用DataKeys[index]
。根据MSDN,当您设置DataKeyNames
属性时,将填充DataKeys
对象。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys(v=vs.100).aspx。尝试设置该属性,在on MSDN.