GridView
列を手動で生成しているときに問題が発生しました。私のプログラムは次の行で停止します:
e.Row.Cells[42].Text = "x";
そして、以下のエラーをスローします:
「指定された引数は有効な値の範囲外でした。パラメータ名: index 説明: 現在の Web 要求の実行中に未処理の例外が発生しました。エラーの詳細とコードのどこで発生したかについては、スタック トレースを確認してください。
例外の詳細: System.ArgumentOutOfRangeException: 指定された引数が有効な値の範囲外でした。パラメータ名: index"
私の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";
)、エラーが発生します。なぜこれが起こるのですか?