当GridView.AutoGenerateColumns为false时,是否有任何单元格数限制?

时间:2016-07-20 08:23:02

标签: asp.net gridview gmgridviewcell

我手动生成GridView列时遇到问题。 我的程序停在这一行:

e.Row.Cells[42].Text = "x";

并抛出以下错误:

  

"指定的参数超出了有效值的范围。参数   name: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";上)时,会发生错误。为什么会这样?

1 个答案:

答案 0 :(得分:0)

如果你没有十亿行,那么列数不是问题 (来源:http://forums.asp.net/post/3380845.aspx

你当然只有42列,当你想要达到第43列时,你会得到ArugmentOutOfRangeException。