如何解决System.ArgumentOutOfRangeException异常?

时间:2017-02-09 08:50:46

标签: c# asp.net gridview

我有一个包含多个数据列的网格和一个带复选框的列。有一个名为app Number的列包含数字。现在我想在选中的行中选择数字并将它们放入数组。我该怎么做?

这是我到目前为止所做的代码。

<asp:GridView ID="gvAppeals" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4"
    Width="665px" AutoGenerateColumns="False" OnSelectedIndexChanged="gvAppeals_SelectedIndexChanged1"
    AllowPaging="True" PageSize="10" OnPageIndexChanging="gvAppeals_PageIndexChanging" OnRowDataBound="gvAppeals_RowDataBound">
    <Columns>
        <asp:BoundField DataField="App_no" HeaderText="APP NO">
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle Font-Size="Small" HorizontalAlign="Left" VerticalAlign="Middle" />
        </asp:BoundField>
        <asp:BoundField DataField="EMP_FULLNAME" HeaderText="FULL NAME">
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle Font-Size="Small" HorizontalAlign="Left" VerticalAlign="Middle" />
        </asp:BoundField>
        <asp:BoundField DataField="EMP_NIC_NO" HeaderText="NIC">
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle Font-Size="Small" HorizontalAlign="Left" VerticalAlign="Middle" />
        </asp:BoundField>
        <asp:BoundField DataField="EMP_BIRTHDAY" HeaderText="BIRTH DATE" DataFormatString="{0:yyyy-MM-dd}">
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle Font-Size="Small" HorizontalAlign="Left" VerticalAlign="Middle" />
        </asp:BoundField>
        <asp:TemplateField>
            <%--<HeaderTemplate>
                            <asp:CheckBox ID="chkHeader" runat="server" AutoPostBack="true" />
                        </HeaderTemplate>--%>
            <ItemTemplate>
                <asp:CheckBox ID="chkSelect" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
    <RowStyle BackColor="White" ForeColor="Black" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
    <SortedAscendingCellStyle BackColor="#FEFCEB" />
    <SortedAscendingHeaderStyle BackColor="#AF0101" />
    <SortedDescendingCellStyle BackColor="#F6F0C0" />
    <SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>

C#代码

protected void btnConfirm_Click(object sender, EventArgs e)
    {
        //DataSet ds = new DataSet();
        int[] numbers;
        numbers = new int[gvAppeals.Rows.Count];
        int noOfRowsChecked = 0;

        foreach (GridViewRow row in gvAppeals.Rows)
        {
            int rowIndex = row.RowIndex;
            CheckBox chkrow = (CheckBox)row.FindControl("chkSelect");

            if (chkrow.Checked == true)
            {
                numbers[noOfRowsChecked] = Int32.Parse(gvAppeals.DataKeys[rowIndex]["App_no"].ToString());
                noOfRowsChecked++;
            }

            //update the dept by checking the selected appeal numbers 
            if (noOfRowsChecked > 0)
            {
                for (int i = 0; i < numbers.Length; i++)
                {
                    int appNo = numbers[i];
                    dba.confirmAppeal(appNo);
                }
            }
            else
            {
                WebMsgBox.Show("Please select an application to confirm");
            }
        }
    }

2 个答案:

答案 0 :(得分:2)

温和提醒您需要在索引上减去一个(-1)。这可能是您获得System.ArgumentOutOfRangeException的原因。

答案 1 :(得分:0)

GridView中没有定义DataKeys,因为它总是超出范围。将DataKeyNames属性添加到GridView。

<asp:GridView ID="gvAppeals" runat="server" DataKeyNames="App_no" >