0

嗨,gusy,我试图了解如何使用带有复选框的grindview 从数据库中删除,但我不知道为什么 deleteresults.text 和 DeleteResults.Visible 不起作用这是我的代码(我是编程新手 :) 并感谢您的回答

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;

}
4

1 回答 1

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。尝试设置该属性,在 MSDN 上阅读更多内容。

于 2013-07-14T06:00:46.837 回答