protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        LinkButton lbtn1 = (LinkButton)GridView1.FindControl("LinkButton1");
        if (lbtn1.CommandName == "Delete")//这就是红字行,
        {
            int index = Convert.ToInt32(lbtn1.CommandArgument);
            string sqlstr = "delete from xianka where id='" + GridView1.Rows[index].ToString() + "'";
            OleDbConnection con = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["pcdb"].ConnectionString);
            OleDbCommand cmd = new OleDbCommand(sqlstr, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            GridView1.DataBind();
        }
    }
LinkButton1是我在模板列中我加的一个linkbutton1,text=删除,commandname设置成Delete,我的目的是点linkbutton1后提示确认删除对话框,前台js实现,这个没问题,就是点击确定后,出现的编译错误!

解决方案 »

  1.   

    要在某一个Cell中找LinkButton比如LinkButton lbtn1 = (LinkButton)GridView1.Rows[e.RowIndex].Cells[0].FindControl("LinkButton1"); 
      

  2.   

    知道了,但是
           int index = Convert.ToInt32(lbtn1.CommandArgument); 这是红字行
    这句又出错了,错误提示
    异常详细信息: System.FormatException: 输入字符串的格式不正确。
      

  3.   

    为什么不用GridView的RowCommand事件
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
            }
        }
      

  4.   

    你的lbtn1.CommandArgument没有赋值
      

  5.   

    在GridView的RowDataBound里面赋值
      

  6.   

    刚刚看错了
    是你这个LinkButton lbtn1 = (LinkButton)GridView1.FindControl("LinkButton1"); 
    lbtn1找不到,你这样是找不到的
      

  7.   

    e.Rows.FindControl();   or  e.Rows.cells[i].FindControl();
      

  8.   

    LinkButton lbtn1 = (LinkButton)GridView1.Rows[e.RowIndex].FindControl("LinkButton1"); 
      

  9.   

    主要这个赋值不太会,我的代码没在RowDataBound里面写。如果在那里面赋值的话应该调用不到吧!
      

  10.   

     
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView drv = (DataRowView)e.Row.DataItem;
                LinkButton lbtn1 = (LinkButton)e.Row.FindControl("LinkButton1"); 
                lbtn1.CommandArgument=drv["id"];
            }
        }   
      

  11.   

    先在GridView1_RowDataBound对其进行赋值,然后在RowCommand事件里写代码可以否?
      

  12.   

    而且我一般把删除操作放到RowCommand事件里面去,这样其他操作都可以写到这里面去    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
        { 
            if (e.CommandName == "Delete") 
            { 
                int id = Convert.ToInt32(e.CommandArgument);
                //删除对应id的数据
             } 
            if (e.CommandName == "View") 
            { 
                int id = Convert.ToInt32(e.CommandArgument);
             } 
            ...
        }
        //或者
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
        {  
            int id = Convert.ToInt32(e.CommandArgument);
            switch(e.CommandName)
            { 
                case "Delete":
                    //删除对应id的数据
                      break;
             } 
        }     
      

  13.   

    我跟你楼上的写的代码一样,但是就是有错误!我把我的贴出来看看!
    if (e.CommandName == "Delete")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                            string sqlstr = "delete from xianka where id='" + GridView1.DataKeys[index].Value.ToString() + "'";
                OleDbConnection con = new OleDbConnection(System.Configuration.ConfigurationManager.ConnectionStrings["pcdb"].ConnectionString);
                OleDbCommand cmd = new OleDbCommand(sqlstr, con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                GridView1.DataBind();        }感觉应该正确,但是倒数第3行报错,是不是SQL语句不对吗?但是应该没错误吧!