I have a problem with a exception below:
index out of range
I have a GridView where each row has a CheckBox. When I select and item in a DropDown list a certain action is performed depending on the ListItem value.
protected void actionSelect_SelectedIndexChanged(object sender, EventArgs e)
{
GridView gv = Project_GridView;
DropDownList ddl = (DropDownList)sender;
int SelectValue = Convert.ToInt32(ddl.SelectedValue);
UpdateOp(gv, SelectValue);
dl_actionSelect.SelectedIndex = 0;
DataBind();
}
This code is run:
private void UpdateOp(GridView gv, int SelectValue)
{
foreach (GridViewRow row in gv.Rows)
{
CheckBox check = (CheckBox)row.FindControl("CB_ActionSelect");
if (check.Checked)
{
int rowIndex = row.RowIndex;
DataBind();
if (!(DBNull.Value == gv.DataKeys[row.RowIndex].Value)) //This line throws the exception
{
int original_id = Convert.ToInt32(gv.DataKeys[row.RowIndex].Value);
//Op_Update
uWeb.Data.ProjLines.Op_Update(SelectValue, original_id);
}
}
}
}
Loops through rows and finds the checked ones. However when more than one checkbox is selected I get:
ArgumentOutOfRangeException: Index is out of range
And the weird part is it seems to be random amount that can successfully run. 1 checked always works, 2+ is rather random sometimes it works sometimes it doesn't. Something with the id for the control?
GridView:
<asp:GridView ID="Project_GridView" runat="server" SkinID="ProdView"
AutoGenerateColumns="False" DataKeyNames="ROWNUMBER" CssClass="gvv"
DataSourceID="Project_ObjectDataSource" OnRowDataBound="Project_GridView_RowDataBound"
AllowSorting="true" Width="100%" AlternatingRowStyle-BackColor="LightGray">
<Columns>
<asp:TemplateField HeaderText="" ItemStyle-Width="4%">
<ItemTemplate>
<asp:CheckBox id="CB_ActionSelect" Text="" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You should get it like this:
string Id = gv.DataKeys[row.RowIndex %= gv.PageSize][0].ToString();
Because, the reported error occurs when the GridView's index increases from the row count.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.