<asp:DataGrid ID="mygrid2" Runat="server" Width="100%" ShowHeader="True" ShowFooter="False" CellPadding="3">
<HeaderStyle BackColor="#FF8080"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="删除" CommandName="del"></asp:ButtonColumn>
<asp:ButtonColumn Text="修改" CommandName="xiugai"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
我需要在点删除的时候对记录进行删除,点修改的时候进行修改,也就是说我想把这个删除和修改事件做成和按钮事件一样,点击的时候进行处理。请问该怎么处理?

解决方案 »

  1.   

    add an ItemCommand event handler for your datagrid, and check the commandName
      

  2.   

    在datagrid的itemCommand事件里判断
    if(e.CommandName=="del")
    {
        //删除操作
    }if(e.CommandName=="xiugai")
    {
        //删除操作
    }
      

  3.   

    参考以下例子:
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %><html>
     <script language="C#" runat="server">    SqlConnection myConnection;    protected void Page_Load(Object sender, EventArgs e)
        {
            myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;uid=sa;pwd=1234");        if (!IsPostBack)
                BindGrid();
        }    public void MyDataGrid_Delete(Object sender, DataGridCommandEventArgs e)
        {
            String deleteCmd = "DELETE from Employee where emp_id = @Id";        SqlCommand myCommand = new SqlCommand(deleteCmd, myConnection);
            myCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 11));
            myCommand.Parameters["@Id"].Value = MyDataGrid.DataKeys[(int)e.Item.ItemIndex];        myCommand.Connection.Open();        try
            {
                myCommand.ExecuteNonQuery();
                Message.InnerHtml = "<b>已删除记录</b><br>" + deleteCmd;
            }
            catch (SqlException)
            {
                Message.InnerHtml = "错误:未能删除记录";
                Message.Style["颜色"] = "红色";
            }        myCommand.Connection.Close();        BindGrid();
        }    public void BindGrid()
        {
            SqlDataAdapter myCommand = new SqlDataAdapter("select * from Employee", myConnection);        DataSet ds = new DataSet();
            myCommand.Fill(ds, "员工");        MyDataGrid.DataSource=ds.Tables["员工"].DefaultView;
            MyDataGrid.DataBind();
        }</script><body style="font: 10.5pt 宋体">  <form runat="server">    <h3><font face="宋体">删除数据行</font></h3>    <span id="Message" EnableViewState="false" style="font: arial 11pt;" runat="server"/><p>    <ASP:DataGrid id="MyDataGrid" runat="server"
          Width="800"
          BackColor="#ccccff"
          BorderColor="black"
          ShowFooter="false"
          CellPadding=3
          CellSpacing="0"
          Font-Name="Verdana"
          Font-Size="8pt"
          HeaderStyle-BackColor="#aaaadd"
          DataKeyField="emp_id"
          OnDeleteCommand="MyDataGrid_Delete"
        >      <Columns>
             <asp:ButtonColumn Text="删除员工" CommandName="Delete"/>
          </Columns>    </ASP:DataGrid>  </form></body>
    </html>
      

  4.   

    删除按钮的CommandName="delete",修改CommandName="edit",更新CommandName="update",取消
    CommandName="cancel"
    注意要小写,
    DataGrid有这几个事件,
      

  5.   

    .net 帮助里自带呀---------------
    欢迊来到麦高网-私活兼职首选平台 www.mgao.net
      

  6.   

    设置你的按钮的commandname属性,下面在有3个BUTTON,设置了commandname属性delete,update,vouch
    private void dgComInfo_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    int id=int.Parse(dgComInfo.DataKeys[e.Item.ItemIndex].ToString());
    if(e.CommandName.Equals("delete"))
    {
    bool result=c.DeleteComInfo(id);
    string str=((TextBox)e.Item.FindControl("TextBox1")).Text;
    string imagepath=Server.MapPath("")+"\\Images\\"+str;
    if(System.IO.File.Exists(imagepath))
    {
    System.IO.File.Delete(imagepath);
    }
    if(result)
    {
    Response.Write("<script>alert('企业名录删除成功')</script>");
    BindData();
    }
    }
    else if(e.CommandName.Equals("update"))
    {
    Response.Redirect("UpdateComInfo.aspx?cid="+id+"");
    }
    else if(e.CommandName.Equals("vouch"))
    {
    Response.Redirect("VouchComInfo.aspx?cid="+id+"");
    }
    }你设置commandname为update ,delete ,也可以使用自己事件里的DeleteCommand和UpdateCommand进行处理,效果是一样的,但是commandname属性一定要是update和delete
    而上面的方法,也就是在itemcommand事件里执行的,属性可以设置为任何的值