rookie
rookie

Reputation: 401

Select button in a grid view

<asp:GridView ID="GridView1"  autogenerateselectbutton="True" selectedindex="0" 
autogeneratecolumns="True" allowpaging="true" runat="server" CssClass="style39" 
datakeynames="Email" RowCommand="GridView1_RowCommand" ShowSelectButton="True">>

I have this select button in my grid view. I have a delete button in the page. I want to delete the selected row but how can I pass the value to the delete function so that the I can write a code to delete the selected row in the database. I want to know which row is selected how to pass the value.

My grid view has a column called email and user name. I want to pass the selected row email. Thanks

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "CommandName")
        {
            String Email = e.CommandArgument.ToString(); // will Return current Row primary key value

            MySqlConnection connectionString = new MySqlConnection("Server=127.0.0.1;Database=surelyknown;Uid=root");
            connectionString.Open();
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            MySqlCommand command = new MySqlCommand();


            command = new MySqlCommand("DELETE from tbl_group, tbl_usergroups using tbl_group inner join tbl_usergroups where tbl_group.GroupID =@Email And tbl_usergroups.tbl_group_GroupID =@Email", connectionString);
            command.Parameters.Add("@Email", MySqlDbType.VarChar, 25);
            command.Parameters["@Email"].Value = Email;

Upvotes: 0

Views: 1848

Answers (2)

Zo Has
Zo Has

Reputation: 13028

  • If you are not going to delete multiple rows at the same time on the gridview a separate delete button is not needed
  • You can simply place the delete button inside a template field in your gridview. That way, you won't need to select a gridview row then press the delete button on your page(2 steps)
  • You can then write a click event for that button & fetch the current gridview row's current index by either using DataKeys or e.CommandArgument

Let me know if you need any more help with this.

Upvotes: 0

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

use RowCommand event of Gridview

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "CommandName")
    {
      String Email = e.CommandArgument.ToString(); // will Return current Row primary key value
      //..Put deletion code here....

      //.....
     }
 }

Upvotes: 1

Related Questions