指数超出范围。必须是非负数且小于集合的大小。设置宽度列

时间:2014-08-29 12:59:43

标签: c# gridview datagridview datagridviewcolumn

我在以编程方式设置GridView列的宽度时遇到问题。我动态地将来自sql源的数据绑定为:

            public void BindGrid()
    {
        SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS; Initial Catalog=PIMS; Integrated Security=true;");
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                String sql = "select [DocumentsID],[Ref],[Subject],[Src],[Dst],[Medium],[Date_Printed],[Date_Received],[Document_Type],[Action_Required],[Due_Date],[Actual_Date],[Content],[Tag],[Issue_No],[Notes],[Assigned_To],[Reply_Ref],[Priority],[Status],[Response],[Physical_File_No],[Physical_Rack_Location],[File_Name] from dbo.Documents";
                cmd.Connection = con;
                cmd.CommandText = sql;
                con.Open();
                DataSet ds = new DataSet();
                using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                {
                    adp.Fill(ds);
                }

                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();                 
            }
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }
    }

我将GridView包装到View工具中,因为(.aspx代码)是:

    <asp:View ID="vGrid1" runat="server">
                &nbsp;<br />
                <asp:Panel ID="pnlGrid1" runat="server" DefaultButton="btnGo">
                    <table class="auto-style1">
                        <tr>
                            <td>&nbsp;</td>
                            <td colspan="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Button ID="btnSearchRecord1" runat="server" Font-Bold="True" ForeColor="#6600CC" Height="33px" OnClick="btnSearchRecord_Click" Text="SEARCH Record" Width="177px" />
                                <asp:Button ID="btnBacktoHome1" runat="server" Font-Bold="True" ForeColor="#FF3300" Height="33px" OnClick="btnBackToHome" Text="Back to home page" Width="178px" />
                            </td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td class="auto-style87">&nbsp;</td>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                        </tr>
                    </table>
                    <asp:Label ID="lblGoToPage" runat="server" Text="Go to page ">
                    </asp:Label>
                    &nbsp;
               <asp:TextBox ID="txtGotoPage" runat="server" Width="47px">
               </asp:TextBox>
                    &nbsp;
               <asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" />
                </asp:Panel>
                <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" AutoGenerateColumns="True" CellSpacing="4" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" DataKeyNames="File_Name" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating" ForeColor="#333333" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnPageIndexChanging="GridView1_PageIndexChanging" AllowSorting="True" OnSorting="GridView1_Sorting" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
                    <PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="10" Position="Bottom" />
                    <AlternatingRowStyle BackColor="White" />
                    <Columns>
                        <asp:TemplateField HeaderText="File">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkbtnFileName" runat="server" CommandArgument='<%# Eval("File_Name") %>' CommandName="Download" Text='<%# Eval("File_Name") %>'></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <EditRowStyle BackColor="#2461BF" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#EFF3FB" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#F5F7FB" />
                    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                    <SortedDescendingCellStyle BackColor="#E9EBEF" />
                    <SortedDescendingHeaderStyle BackColor="#4870BE" />
                </asp:GridView>
                <asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" />
            </asp:View>

RowDataBound事件代码是:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView1.Columns[0].ItemStyle.Width = Unit.Pixel(100);
            GridView1.Columns[1].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[2].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[3].ItemStyle.Width = Unit.Pixel(100);
            GridView1.Columns[4].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[5].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[6].ItemStyle.Width = Unit.Pixel(100);
            GridView1.Columns[7].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[8].ItemStyle.Width = Unit.Pixel(100);
            GridView1.Columns[9].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[10].ItemStyle.Width = Unit.Pixel(100);
            GridView1.Columns[11].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[12].ItemStyle.Width = Unit.Pixel(100);
            GridView1.Columns[13].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[14].ItemStyle.Width = Unit.Pixel(100);
            GridView1.Columns[15].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[16].ItemStyle.Width = Unit.Pixel(100);
            GridView1.Columns[17].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[18].ItemStyle.Width = Unit.Pixel(100);
            GridView1.Columns[19].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[20].ItemStyle.Width = Unit.Pixel(100);
            GridView1.Columns[21].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[22].ItemStyle.Width = Unit.Pixel(100);
            GridView1.Columns[23].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[24].ItemStyle.Width = Unit.Pixel(100);
            GridView1.Columns[25].ItemStyle.Width = Unit.Pixel(200);
            GridView1.Columns[26].ItemStyle.Width = Unit.Pixel(100);
        }
    }

Line 931上的错误消息是:

    Index was out of range. Must be non-negative and less than the size of the collection.

参数名称:index

    Line 930:                GridView1.Columns[0].ItemStyle.Width = Unit.Pixel(100);
    Line 931:                GridView1.Columns[1].ItemStyle.Width = Unit.Pixel(200);
    Line 932:                GridView1.Columns[2].ItemStyle.Width = Unit.Pixel(100);
    Line 933:                GridView1.Columns[3].ItemStyle.Width = Unit.Pixel(200);

尽管在我的代码中给出了GridView列的宽度,但我在浏览器中看不到任何效果。我可能会出错的任何想法?

2 个答案:

答案 0 :(得分:1)

放入调试器并检查数据网格是否具有正确的列。显示您的Gridview没有索引为1的列时出错。

答案 1 :(得分:0)

你确定你有27列吗?

加:你确定&#34; ds.Tables [0]&#34;永远不会是空的?