Reputation: 401
<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
Reputation: 13028
Let me know if you need any more help with this.
Upvotes: 0
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