GridView
手动生成列时出现问题。我的程序停在这一行:
e.Row.Cells[42].Text = "x";
并抛出以下错误:
“指定的参数超出了有效值的范围。参数名称:索引描述:在执行当前 Web 请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.ArgumentOutOfRangeException:指定的参数超出了有效值的范围。参数名称:索引"
我的 HTML 代码是:
<div class="scrolling-container">
<br/>
<asp:GridView EnableSortingAndPagingCallbacks="true" ID="StationGridView" runat="server"
GridLines="None" CellSpacing="1" AlternatingRowStyle-Wrap="false"
Font-Names="tahoma" Font-Size="14px" CellPadding="3"
AllowSorting="True"
ViewStateMode="Disabled" EnableViewState="false"
AllowPaging="True" PageSize="15" dir="rtl" Width="100%"
OnRowDataBound="StationGridView_RowDataBound">
<HeaderStyle BackColor="#d7effd" Font-Bold="true" ForeColor="Navy" Font-Size="12px" Font-Underline="false" />
<FooterStyle BackColor="#d7effd" Font-Bold="true" ForeColor="Navy"
VerticalAlign="Middle" HorizontalAlign="Center" Font-Size="16px" />
<AlternatingRowStyle BackColor="#c0c0c0" ForeColor="#000000" Height="25px" />
</asp:GridView>
</div>
我的源代码是:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
Initialize();
}
//Load data
PortalDataSetTableAdapters.VW_StationTableAdapter stationsAdapter = new PortalDataSetTableAdapters.VW_StationTableAdapter();
PortalDataSet.VW_StationDataTable Station;
Station = stationsAdapter.GetData();
StationGridView.DataSource = Station;
StationGridView.AutoGenerateColumns = true;
StationGridView.DataBind();
}
}
protected void StationGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "a";
e.Row.Cells[1].Text = "b";
e.Row.Cells[2].Text = "c";
.
.
e.Row.Cells[42].Text = "x";
e.Row.Cells[43].Text = "x";
e.Row.Cells[44].Text = "x";
}
}
当生成的单元格数量超过 42 时(正好在行上e.Row.Cells[42].Text = "x";
),就会发生错误。为什么会出现这种情况?