我在以编程方式设置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">
<br />
<asp:Panel ID="pnlGrid1" runat="server" DefaultButton="btnGo">
<table class="auto-style1">
<tr>
<td> </td>
<td colspan="2"> <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> </td>
</tr>
<tr>
<td> </td>
<td class="auto-style87"> </td>
<td> </td>
<td> </td>
</tr>
</table>
<asp:Label ID="lblGoToPage" runat="server" Text="Go to page ">
</asp:Label>
<asp:TextBox ID="txtGotoPage" runat="server" Width="47px">
</asp:TextBox>
<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列的宽度,但我在浏览器中看不到任何效果。我可能会出错的任何想法?
答案 0 :(得分:1)
放入调试器并检查数据网格是否具有正确的列。显示您的Gridview没有索引为1的列时出错。
答案 1 :(得分:0)
加:你确定&#34; ds.Tables [0]&#34;永远不会是空的?