这是gridview中删除代码,现在的问题是红线部分取不到值,请问问大家,选择要进行删除操作的这一行时,第一元素jh值已经取到了,sgmc和fssj应该怎么取呢?笨猪难住了!protected void accidentInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {            string jh = accidentInfo.DataKeys[e.RowIndex].Values[0].ToString();
            string sgmc = accidentInfo.Rows[e.RowIndex].Cells[1].Text;
            string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text;

    
            string SqlStr = "delete from GCSG where filename='" +fssj+jh +sgmc+ "'";
            try
            {
                OracleConnection conn = DB.createConnection();
                if (conn.State.ToString() == "Closed") conn.Open();
                OracleCommand comm = new OracleCommand(SqlStr, conn);
                OracleDataReader r = comm.ExecuteReader();
                r.Read();
                if (r.HasRows)
                {                 Response.Write("<script language =javascript>window.alert('删除成功!');</script>");
                }                if (conn.State.ToString() == "Open") conn.Close();                accidentInfo.EditIndex = -1;
                gridViewDataBind();
                Response.Redirect("../accidentQuery/jingHao_Query.aspx");            }
            catch (Exception ex)
            {
                Response.Write("数据库错误,错误原因:" + ex.Message);
                Response.End();
            }        
    }

解决方案 »

  1.   

       string sgmc = accidentInfo.Rows[e.RowIndex].Cells[1].Text; 
                string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text; 
    貌似代码没错
      

  2.   

    感觉这句SQL有问题string SqlStr = "delete from GCSG where filename='" +fssj+jh +sgmc+ "'"; 
      

  3.   


    你SQL delete 什么了?
      

  4.   

    在GridView中Label控件,然后去控件的值
      

  5.   

    参考:
    http://blog.csdn.net/insus/archive/2008/03/30/2229898.aspx另外,如果是DataList:
    http://www.cnblogs.com/insus/articles/1443147.html
      

  6.   

    是不是你的Cell【1】和Cell【2】是模板列來著啊,
    做好的辦法是將你的Cell【1】,Cell[2]的值輸出來,看是否取到值,取值正確的話檢查SqL羅。
      

  7.   

    打个断点!accidentInfo.Rows[e.RowIndex].Cells[2]的值你是不是存控件里了?打个断断点看下取没取到值
      

  8.   

    在模板列里加个
    <asp:LinkButton ID="DeleteTask" runat="server" CommandName="delete" CommandArgument='<%# Eval("ID")%>' Text="删除" OnClientClick="return confirm('确定要删除该任务信息吗?')" />
    后台在ItemCommand中写方法protected void repReport_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string ID = e.CommandArgument.ToString();        if (e.CommandName == "delete")
            {
                del()//你的删除方法,上边的ID是删除数据的ID;
            }
            Bind();
        }
    注意:我这个是在Repeater中实现的很简单。
      

  9.   


    <asp:Repeater ID="repReport" runat="server" OnItemCommand="repReport_ItemCommand">
                <HeaderTemplate>
                    <tr class="title">
                        <td width="1%">
                            组ID
                        </td>
                        <td width="2%">
                            组中文描述
                        </td>
                        <td width="1%">
                            操作
                        </td>
                    </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr class="tdbg" onmouseover="this.className='tdbgmouseover'" onmouseout="this.className='tdbg'">
                        <td>
                            <%# Eval("GroupId")%>
                        </td>
                        <td>
                            <%# Eval("GroupExp")%>
                        </td>
                        <td>
                            <asp:LinkButton ID="DeleteTask" runat="server" CommandName="delete" CommandArgument='<%# Eval("OpName")%>'
                                Text="删除" OnClientClick="return confirm('确定要删除该任务信息吗?')" />
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
      

  10.   

    上边那个不全呵呵<table cellpadding="0" cellspacing="1" class="border" align="center" id="tbReport">
            <asp:Repeater ID="repReport" runat="server" OnItemCommand="repReport_ItemCommand">
                <HeaderTemplate>
                    <tr class="title">
                        <td width="1%">
                            组ID
                        </td>
                        <td width="2%">
                            组中文描述
                        </td>
                        <td width="1%">
                            操作
                        </td>
                    </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr class="tdbg" onmouseover="this.className='tdbgmouseover'" onmouseout="this.className='tdbg'">
                        <td>
                            <%# Eval("GroupId")%>
                        </td>
                        <td>
                            <%# Eval("GroupExp")%>
                        </td>
                        <td>
                            <asp:LinkButton ID="DeleteTask" runat="server" CommandName="delete" CommandArgument='<%# Eval("OpName")%>'
                                Text="删除" OnClientClick="return confirm('确定要删除该任务信息吗?')" />
                        </td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
        </table>
      

  11.   

    string sgmc = accidentInfo.Rows[e.RowIndex].Cells[1].Text; 
    string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text; 先將sgmc,fssj Response.Write()出來
    如果有值,就應該是sql語句有問題強烈建議
    string sgmc = accidentInfo.Rows[e.RowIndex].Cells[1].Text.Trim(); 
    string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text.Trim(); 
      

  12.   

    accidentInfo.Rows[e.RowIndex].Cells[1]是什么类型的,强制类型转换一下再取值试试。
      

  13.   

    你的SQL语句 
    先定义要相加的值,然后在写SQL语句!
      

  14.   

    string sgmc = accidentInfo.Rows[e.RowIndex].Cells[1].FindControl ???
    是不是加了什么东西在模版列里面?
      

  15.   

    1.你gridview是怎么绑定的?模版列手动绑定还是自动绑定?
    若用模版列OnRowDataBond()绑定 个人感觉你的取值方法不正确 尝试:
    string sgmc = accidentInfo.Rows[e.RowIndex]。FindControl(“控件名”).Text。tostring() 
    2. string SqlStr = "delete from GCSG where filename='" +fssj+jh +sgmc+ "'";看着怪怪的 尝试下这样写试试:
    string abc=fss+jh+sgmc;
    string SqlStr = "delete from GCSG where filename='" + abc + "'" ;
    暂时只想到这么多
      

  16.   

    不好意思 我打错了 是RowDataBound
      

  17.   

      //单行删除
        protected void gvAfterService_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(gvAfterService.DataKeys[e.RowIndex].Value.ToString());
            string result = after.AfterServiceDel(id);
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + result + "');</script>");
            Bind();
        }
        //全选删除
        protected void lbtDel_Click(object sender, EventArgs e)
        {
            string result = "";
            foreach (GridViewRow row in gvAfterService.Rows)
            {
                if ((row.FindControl("CheckBox1") as CheckBox).Checked)
                {
                    int id = int.Parse(gvAfterService.DataKeys[row.RowIndex].Value.ToString());
                    result = after.AfterServiceDel(id);
                }
            }
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + result + "');</script>");
            Bind();
        }
      

  18.   

    我知道是什么问题了  楼主啊  你用gridview的时候判断下是否为数据行  按你上面那么些  第一次 是取不到值的  因为根本就不是数据行  而是标题行啊
    判断是否为数据行
    if (e.Row.RowType==DataControlRowType.DataRow)
    {
        string jh = accidentInfo.DataKeys[e.RowIndex].Values[0].ToString(); 
              string sgmc = accidentInfo.Rows[e.RowIndex].Cells[1].Text; 
                string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text; 
        
                string SqlStr = "delete from GCSG where filename='" +fssj+jh +sgmc+ "'"; 
                try 
                { 
                    OracleConnection conn = DB.createConnection(); 
                    if (conn.State.ToString() == "Closed") conn.Open(); 
                    OracleCommand comm = new OracleCommand(SqlStr, conn); 
                    OracleDataReader r = comm.ExecuteReader(); 
                    r.Read(); 
                    if (r.HasRows) 
                    {                 Response.Write(" <script language =javascript>window.alert('删除成功!'); </script>"); 
                    }                 if (conn.State.ToString() == "Open") conn.Close();                 accidentInfo.EditIndex = -1; 
                    gridViewDataBind(); 
                    Response.Redirect("../accidentQuery/jingHao_Query.aspx"); 
                } 
                catch (Exception ex) 
                { 
                    Response.Write("数据库错误,错误原因:" + ex.Message); 
                    Response.End(); 
                } }
      

  19.   

    用dataitem.datarowview这个行吗,昨天我也是这个问题,之后调用gridview.dataitem.datarowview[index]来取值就行了
      

  20.   

    我的这个问题解决了,出来告诉大家
     string jh = accidentInfo.DataKeys[e.RowIndex].Values[0].ToString(); 这个不变
    下面的两个取值把
              string sgmc = accidentInfo.Rows[e.RowIndex].Cells[1].Text; 
                string fssj = accidentInfo.Rows[e.RowIndex].Cells[2].Text; 
    变成
    string sgmc = accidentInfo.DataKeys[e.RowIndex].Values[1].ToString(); 
    string fssj = accidentInfo.DataKeys[e.RowIndex].Values[2].ToString(); 
    然后再 前台gridview的datakeys中增加 sgmc和fssj