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

在我的网格视图中有这个选择按钮。我在页面中有一个删除按钮。我想删除选中的行,但是如何将值传递给删除函数,以便我可以编写代码来删除数据库中选定的行。我想知道哪一行被选中如何传递值。在网格视图中选择按钮

我的网格视图有一个名为电子邮件和用户名的列。我想传递选定的行电子邮件。由于

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 protected] And tbl_usergroups.tbl_group_GroupID [email protected]", connectionString); 
      command.Parameters.Add("@Email", MySqlDbType.VarChar, 25); 
      command.Parameters["@Email"].Value = Email; 

回答

1

使用RowCommand的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.... 

     //..... 
    } 
} 
+0

执行没有调用GridView1_RowCommand – rookie

+0

在页面加载中,您再次绑定数据,它应该像...... if(!IspostBack){绑定数据gridview在这里..} –

0
  • 如果你不打算在同一时间在GridView上不需要一个单独的按钮来删除多行
  • 你可以只需在GridView中的模板字段中放置删除按钮即可。这样,你不需要选择一个gridview行,然后按下页面上的删除按钮(2步)
  • 然后,您可以编写该按钮的点击事件&通过使用来获取当前gridview行的当前索引DataKeyse.CommandArgument

让我知道如果你需要任何这方面更多的帮助。

相关问题