我正在尝试根据该行的特定column(column Id)
的值从gridview中删除特定行。
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();
}
我收到错误:
指数超出范围。必须是非负数且小于 集合。参数名称:index描述:未处理 在执行当前Web请求期间发生异常。 请查看堆栈跟踪以获取有关错误的更多信息 它起源于代码。
异常详细信息:System.ArgumentOutOfRangeException:索引已经用完 范围。必须是非负的且小于的大小 采集。参数名称:index
来源错误:
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);
答案 0 :(得分:0)
试试这个
string id = grd_issuesDetails.Rows[e.RowIndex].Cells[your cell vaule index like 1,2... whatever].Text.Trim();
只需将ID传递给您的查询
答案 1 :(得分:0)
我设法通过使用:
来解决它 int id = Convert.ToInt32(grd_issuesDetails.DataKeys[e.RowIndex].Values[0]);
SqlCommand cmd = new SqlCommand("Delete from [Issuereport] where id= '"+id +"' ", con);