Asp.net C# get GridView cell value and use it in a button column event
For one of my school projects i'm coding cms portal with asp.net. I need to post this article because all of these solutions about grid view tool on web is just shit. Trying to them is just waste of time and i already lost my hours with trying these shits.Â
First we add a gridview on project. Than on page.aspx.cs and add the code below:
public void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
      if (e.CommandName == "delete")
      {
        int currentRowIndex = Convert.ToInt32(e.CommandArgument.ToString());
        id = Convert.ToInt32(GridView1.Rows[currentRowIndex].Cells[2].Text); // this is where we get gridview cell value
         myUser = new user(Convert.ToInt32(id));
        myUser.deleteUser();Â
     }
     }
Now we should code a function for deleting row:
protected void GridView1_RowDeleting(object sender, EventArgs e)
    {
      listAllUsers();
    }
Finally we need to add options below for gridview tool on page.aspx source
onrowcommand="GridView1_RowCommand"
onrowdeleting="GridView1_RowDeleting"
Thats excatly what we need to do.Â













