【发布时间】:2014-02-09 19:20:06
【问题描述】:
我有一个gridview,我有一个数据库。在我的任务中,我将 GridView 绑定到 DB,并希望更改每列的宽度。
dataAdapter = new SqlDataAdapter("SELECT * FROM Turs", sqlcn);
dt = new DataTable("Turs");
dataAdapter.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
如果我将代码添加到 GridView1_RowDataBound 中,我会收到错误消息:“Specified argument was out of the range of valid values. Parameter name: index”。调试器的跟踪显示 GridView1 只有 1 列。为什么?在 DB 中,我有 8 列。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Width = 100;
e.Row.Cells[1].Width = 150;
}
问候
编辑:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" Font-Size="Medium"
ShowHeaderWhenEmpty="True" AutoGenerateColumns="True"
onrowdatabound="GridView1_RowDataBound">
<EditRowStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" />
<HeaderStyle Font-Bold="True" Font-Size="Larger" ForeColor="Blue" />
<RowStyle BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" />
</asp:GridView>
【问题讨论】:
-
能否请您从aspx页面添加Gridview的代码?