I have a problem when I am generating a GridView
column manually.
my program stops at this line:
e.Row.Cells[42].Text = "x";
and throws the below error:
"Specified argument was out of the range of valid values. Parameter name: index Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index"
My HTML code is:
<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>
My source code is :
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";
}
}
When the number of cells are generating goes more than 42 (exactly on the line e.Row.Cells[42].Text = "x";
), the error happens. Why does this occur?