使用复选框从gridview中删除

时间:2013-07-14 01:27:19

标签: c# asp.net

嗨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;

}

1 个答案:

答案 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.

上阅读更多内容