在控件的属性栏“事件”中有EditCommand/DeleteCommand/CancelCommand 事件的,双击添加你的代码,按钮就做你要它做的事情了。

解决方案 »

  1.   

    http://xml.sz.luohuedu.net/xml/ShowDetail.asp?id=8ADE535F-AD40-4DE3-A962-A64B4FAF12C4
      

  2.   

    private void MyDataGrid_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {  
    string  SQLStatement="DELETE from sys_select where code = @code" ;          //通过主键列code删除
    string  myConnectionString  =  ConfigurationSettings.AppSettings["ConnectString"]; 
    SqlConnection  myConnection  =  new  SqlConnection(myConnectionString);  
    SqlCommand  myCommand  =  new  SqlCommand  (SQLStatement,myConnection);    
    myCommand.Parameters.Add(new SqlParameter("@code", SqlDbType.VarChar ,20));  
    myCommand.Parameters["@code"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex]; //取得选中行的主键值
    myCommand.CommandTimeout  =  15;   
    myCommand.CommandType=CommandType.Text;    try   
    {   
    myConnection.Open();   
    myCommand.ExecuteNonQuery();   
    myConnection.Close();  
    BindGrid();  
    }   
    catch  
    {    if(MyDataGrid.CurrentPageIndex>=MyDataGrid.PageCount) //控制最后一条纪录删除不出错
    {
    int i=MyDataGrid.CurrentPageIndex;
    i--;
    MyDataGrid.CurrentPageIndex=i;
    BindGrid();   
    }
      
    }   
    // MyDataGrid.EditItemIndex  =  -1;   
    // BindGrid(); 
    }