代码之家  ›  专栏  ›  技术社区  ›  Anita Mathew

无法从gridview获取行的id值以删除特定行

  •  -1
  • Anita Mathew  · 技术社区  · 7 年前

    我正在尝试根据特定行的值从gridview中删除特定行 column(column Id) 对于那一排。

    gridview如下所示:

     <asp:GridView ID="grd_issuesDetails" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnRowDataBound="grd_issuesDetails_RowDataBound"
                                     GridLines="None" Width="600px" CellPadding="4" ForeColor="#333333" OnPageIndexChanging="grd_issuesDetails_PageIndexChanging"  DataKeyNames="id"  OnSelectedIndexChanged="grd_issuesDetails_SelectedIndexChanged"     onrowdeleting="grd_issuesDetails_RowDeleting">
                                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                    <Columns>
                                        <asp:BoundField DataField="id" HeaderText="Id" ><ItemStyle HorizontalAlign="Center" /></asp:BoundField>
                                         <asp:BoundField DataField="img_name" HeaderText="Image name" ><ItemStyle HorizontalAlign="Center" /></asp:BoundField>
    
                                         <asp:BoundField DataField="teacher_code" HeaderText="Teacher code" ><ItemStyle HorizontalAlign="Center" /></asp:BoundField>
                                         <asp:BoundField DataField="issue_date" HeaderText="Reported date" ></asp:BoundField>     
                                         <asp:TemplateField HeaderText="Resolve">
                                         <ItemTemplate>  
                                         <asp:Button ID="Buttonid"   runat="server" CommandName="fetch" Text="Fetch" OnClick="Button_fetch"></asp:Button>                                  
    
                                        </ItemTemplate>
                                        </asp:TemplateField>
    
                                               <asp:BoundField DataField="status" HeaderText="Status" />
    
                                               <asp:TemplateField>
    
        <ItemTemplate>
    
        <asp:ImageButton ID="img_user" runat="server" CommandName="Select"  ImageUrl='<%# Eval("Status") %>' Width="20px" Height="20px" />
    
            </ItemTemplate>
    
        </asp:TemplateField>
    
                                        <asp:TemplateField HeaderText="Action">
    
                        <ItemTemplate>
    
    
    
                           <asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete" Width="20px" Height="20px" ImageUrl="~/Images/Delete.png"/>
    
                        </ItemTemplate>
    
                        <EditItemTemplate>
    
                           <asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="~/Images/icon-update.png"/>
    
                           <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/icon-Cancel.png"/>
    
                        </EditItemTemplate>
    
                        <FooterTemplate>
    
                           <asp:LinkButton ID="lbtnAdd" runat="server" CommandName="ADD" Text="Add" ></asp:LinkButton>
    
                        </FooterTemplate>
    
                    </asp:TemplateField>                    
                                    </Columns>
                                    <EditRowStyle BackColor="#999999" />
                                    <FooterStyle BackColor="#5D7B9D" Font-Bold="false" ForeColor="White" />
                                    <HeaderStyle BackColor="#0000CD" Font-Bold="false" ForeColor="white" />
                                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="false" ForeColor="#333333" />
                                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                                     <PagerStyle HorizontalAlign = "Right" CssClass = "GridPager" />
                                </asp:GridView>
    

    代码隐藏:

     protected void grd_issuesDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
    
            string id = grd_issuesDetails.DataKeys[grd_issuesDetails.SelectedIndex].Value.ToString();
    
            //int row = Convert.ToInt32(grd_issuesDetails.DataKeys[e.RowIndex].Value);
            SqlCommand cmd = new SqlCommand("Delete from [Issuereport] where id= '"+id+"' ", con);
    
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
    
    
        }
    

    我得到错误:

    索引超出范围。必须为非负数且小于 集合。参数名称:索引描述:未处理的 执行当前web请求期间发生异常。 请查看堆栈跟踪以了解有关错误和 它起源于代码。

    异常详细信息:系统。ArgumentOutOfRangeException:索引已出 范围的。必须为非负数且小于 收集参数名称:索引

    源错误:

    Line 123:        {
    Line 124:
    Line 125:            string id = grd_issuesDetails.DataKeys[grd_issuesDetails.SelectedIndex].Value.ToString();
    Line 126:
    Line 127:            //int row = Convert.ToInt32(grd_issuesDetails.DataKeys[e.RowIndex].Value);
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Tejash Raghav    7 年前

    试试这个

    string id = grd_issuesDetails.Rows[e.RowIndex].Cells[your cell vaule index like 1,2... whatever].Text.Trim();
    

    只需将id传递给您的查询

        2
  •  0
  •   Anita Mathew    7 年前

    我通过以下方式解决了此问题:

      int id = Convert.ToInt32(grd_issuesDetails.DataKeys[e.RowIndex].Values[0]);
            SqlCommand cmd = new SqlCommand("Delete from [Issuereport] where id= '"+id +"' ", con);