我正在为此使用GridView模板字段。我想更新“权限”列中的数据。但是我收到错误消息“索引超出范围。必须为非负数,并且小于集合的大小。参数名称:index”
下面是后端代码。
protected void gvRequestStatus_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
using (SqlConnection con = db.ConnectToDb())
{
con.Open();
string query = "UPDATE Shipment SET Permission=@status WHERE ShipmentID=@id;";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@status", (gvRequestStatus.Rows[e.RowIndex].FindControl("ddlPermission") as DropDownList).SelectedIndex.ToString());
cmd.Parameters.AddWithValue("@id", (gvRequestStatus.DataKeys[e.RowIndex].Value.ToString()));
cmd.ExecuteNonQuery();
lblSuccessMessage.Text = "Status Updated";
lblErrorMessage.Text = "";
gvRequestStatus.EditIndex = -1;
PopulateGridView();
}
}
catch (Exception ex)
{
lblSuccessMessage.Text = "";
lblErrorMessage.Text = ex.Message;
}
}
我在哪里做错了?
以下是前端(gridview)代码
<asp:GridView ID="gvRequestStatus" runat="server" Width="85%" EmptyDataText="No Record Found" CssClass="gvCss" HeaderStyle-CssClass="header-center" AutoGenerateColumns="False" OnRowCommand="gvRequestStatus_RowCommand" OnRowEditing="gvRequestStatus_RowEditing" OnRowCancelingEdit="gvRequestStatus_RowCancelingEdit" OnRowUpdating="gvRequestStatus_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="Shipment ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("ShipmentID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Departing Port">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("DepartPort") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Arriving Port">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("ArrivalPort") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Requested Date">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("RequestDate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Remarks">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("Remarks") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("Permission") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlPermission" Text='<%#Eval("Permission") %>' runat="server">
<asp:ListItem Text="Pending" Value="Pending"/>
<asp:ListItem Text="Approved" Value="Approved"/>
<asp:ListItem Text="Rejected" Value="Rejected"/>
<asp:ListItem Text="Shipping" Value="Shipping"/>
<asp:ListItem Text="Delivered" Value="Delivered"/>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cost">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("Cost") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User ID">
<ItemTemplate>
<asp:Label runat="server" Text='<%#Eval("UserID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField >
<ItemTemplate>
<asp:ImageButton runat="server" ImageUrl="~/Pics/edit.png" CommandName="Edit" ToolTip="Edit" width="20px" Height="20px"/>
<%--<asp:ImageButton runat="server" ImageUrl="~/Pics/delete.png" CommandName="Delete" ToolTip="Delete" width="20px" Height="20px"/>--%>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton runat="server" ImageUrl="~/Pics/save.png" CommandName="Update" ToolTip="Edit" width="20px" Height="20px"/>
<asp:ImageButton runat="server" ImageUrl="~/Pics/cancel.png" CommandName="Cancel" ToolTip="Cancel" width="20px" Height="20px"/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Center" />
<rowstyle Height="40px"/>
</asp:GridView>