我在datagrid的属性生成器里加了一个按钮列中的删除列,我想取得删除行的id,然后根据id删除整行,我写了下面这段代码:
string id=e.Item .Cells[0].Text;
SqlConnection con=new SqlConnection("server=.;database=winter;uid=sa;pwd=11");
con.Open();
SqlCommand cmd=new SqlCommand("delete from publishers where pub_id='"+id+"'",con);
cmd.ExecuteNonQuery();但是我不知道这些代码应该放在哪里,因为不能点击删除进去编写代码,到底怎么办啊???没分了,大家帮帮我吧

解决方案 »

  1.   

    是在DataGrid1_ItemCommand事件还是DataGrid1_DeleteCommand事件中写程序??
      

  2.   

    private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    SqlConn ss=new SqlConn();
    string strCmd="delete from tablename where id ='"+DataGrid1.DataKeys[(int)e.Item.ItemIndex]+"'";
    SqlConnection sc=new SqlConnection("server=.;database=winter;uid=sa;pwd=11");
    ss.conToSql(sc);
    SqlCommand delCmd=new SqlCommand(strCmd,sc);
    try
    {
    sc.Open();
    delCmd.ExecuteNonQuery(); DataGridDataBind();
    }
    catch(Exception err)
    {
    Response.Write("删除时出错,原因为:"+err.ToString());
    }
    finally
    {
    sc.Close(); }
    }
    我这样写的,通过。
      

  3.   

    boci,我照你的做了,提示:c:\inetpub\wwwroot\hyperlink\WebForm1.aspx.cs(93): “System.Data.SqlClient.SqlConnection”并不包含对“conToSql”的定义
      

  4.   

    选中datagrid,看它的属性.点击 闪电符号 .找到deletecommand 双击,然后把代码放到这个函数中就可以了~~~
      

  5.   

    private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if (e.CommandName=="Delete")
    {

    string name=e.Item.Cells[1].Text;
        this.Label1.Text=name;
    }
    }我在页面上放了一个标签,用来检验取没取得行值,但是label没变化,用
    string name=e.Item.Cells[1].Text;来取得行值不对么??