RowIndex of Gridview Error in asp.net

只愿长相守 提交于 2019-12-12 06:06:34

问题


I am implementing the bulk delete functionality with the help of Checkbox. But when I call the ID like below

string Id = grdUser.DataKeys[e.RowIndex].Value.ToString();

I get the error as

System.EventArgs does not contain a definition of RowIndex.

I dont know why it is happening. Please see my code for your reference:-

 protected void btnDelete_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvRow in grdUser.Rows)
        {
            CheckBox chkDelete = (CheckBox)grdUser.FindControl("chkDelete");
            if (chkDelete.Checked)
            {
                string Id = grdUser.DataKeys[e.RowIndex].Value.ToString();
            }
        }
    }

Do let me know what changes I have to make


回答1:


You should add the gvRow.RowIndex as stated by Sandeep.

Then you have to bind your gridview somewhat like this.

 protected void btnDelete_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvRow in grdUser.Rows)
        {
            if (gvRow.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkDelete = (CheckBox)gvRow.FindControl("chkDelete");
                if (chkDelete.Checked)
                {
                    string Id = grdUser.DataKeys[gvRow.RowIndex].Value.ToString();
                    DeleteRecordByID(Id);
                }
            }
        }

       //Bind your Gridview here
    }

Let me know if it works or not




回答2:


you can Use Below Code

for (int i = 0; i < grdUser.Rows.Count; i++)
{
    //Your logic and use   grdUser.DataKeys[i].Value.ToString();
      for delete


}


来源:https://stackoverflow.com/questions/27566544/rowindex-of-gridview-error-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!