C# 如何解决System.ArgumentOutOfRangeException异常?
我有一个带有多个数据列的网格和一个带有复选框的列。有一个名为appnumber的列,其中包含数字。现在我想选择选中行中的数字,并将它们放入数组中。我怎么做C# 如何解决System.ArgumentOutOfRangeException异常?,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个带有多个数据列的网格和一个带有复选框的列。有一个名为appnumber的列,其中包含数字。现在我想选择选中行中的数字,并将它们放入数组中。我怎么做 <asp:GridView ID="gvAppeals" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="665px" AutoGenerateCo
<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>
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");
}
}
}
这是我到目前为止所做的代码
<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>
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");
}
}
}
C#代码
<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>
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");
}
}
}
protectedvoid btnConfirm\u单击(对象发送方,事件参数e)
{
//数据集ds=新数据集();
int[]数字;
数字=新整数[gv.Rows.Count];
int noOfRowsChecked=0;
foreach(gridview.Rows中的行)
{
int rowIndex=row.rowIndex;
复选框chkrow=(复选框)row.FindControl(“chkSelect”);
如果(chkrow.Checked==true)
{
numbers[noOfRowsChecked]=Int32.Parse(gv32.DataKeys[rowIndex][“App_no”].ToString());
noOfRowsChecked++;
}
//通过检查所选上诉编号更新部门
如果(noOfRowsChecked>0)
{
for(int i=0;i
只是一个温和的提醒,你需要在索引中减去一(-1)。这可能就是您获得System.ArgumentOutOfRangeException的原因。这只是一个温和的提醒,提醒您需要在索引中减去一(-1)。这可能就是您获取系统的原因。ArgumentOutOfRangeExceptionGridView中没有定义数据键,因此它总是超出范围。将DataKeyNames
属性添加到GridView
<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>
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");
}
}
}
<asp:GridView ID="gvAppeals" runat="server" DataKeyNames="App_no" >
GridView中没有定义
数据键,因此它总是超出范围。将DataKeyNames
属性添加到GridView
<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>
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");
}
}
}
<asp:GridView ID="gvAppeals" runat="server" DataKeyNames="App_no" >
我会把int[]numbers
变成一个列表list numbers=new list()
然后用numbers.Add()
我现在就试试。我会把int[]numbers
变成一个列表list numbers=new list()
然后用numbers.Add()
我现在就试试。这取决于用法。如果在for
循环中减去-1
,像这样for(int i=0;i<(numbers.Length-1);i++)
它将永远不会到达最后一个元素。请尝试在代码上添加断点,并检查得到错误的行。在下面的行中获取错误------->numbers[noOfRowsChecked]=Int32。解析(gvApps.DataKeys[rowIndex][“应用程序编号”].ToString());如果这是您获得System.ArgumentOutOfRangeException
,请尝试将其更改为数字[NoofRowChecked]=Int32.Parse(gvApps.DataKeys[rowIndex-1][“应用程序编号”].ToString());代码>这取决于用法。如果在-1
for
循环中减去-1
,像这样for(int i=0;i<(numbers.Length-1);i++)
它将永远不会到达最后一个元素。尝试在代码上添加断点,并检查得到错误的行。在下面的行中获取错误------->numbers[noOfRowsChecked]=Int32.Parse(gvwatches.DataKeys[rowIndex][“App_-no”].ToString());如果这是您获得的System.ArgumentOutOfRangeException
,请尝试将其更改为数字[noOfRowsChecked]=Int32.Parse(gvwatches.DataKeys[rowIndex-1][“App_-no”].ToString());代码>