1

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?

Draken
  • 3,134
  • 13
  • 34
  • 54
Afagh johari
  • 9
  • 1
  • 3
  • Because you only have 42 cells in that row (41 + the 0th row, makes 42)? Why do you think you have more than 42 cells in that row? – Draken Jul 20 '16 at 08:28

1 Answers1

0

If you don't have billion of rows, columns count is not the problem here (source: http://forums.asp.net/post/3380845.aspx )

You certainly have only 42 columns, and when you're trying to reach the 43th, you get the ArugmentOutOfRangeException.

Michel Amorosa
  • 475
  • 5
  • 11
  • my query has about more than 1000 record but when I fetch the number of records for my grid view it is 42, I don't know why?. even when I execute my query in my model(mydataset.xsd file in app_code folder) it returns complete data, – Afagh johari Jul 31 '16 at 09:31
  • So now your problem is the number of rows ? (a record = a row ?) I don't know how to help you, sorry – Michel Amorosa Aug 01 '16 at 19:17