【问题标题】:something wrong with gridview网格视图有问题
【发布时间】: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的代码?

标签: c# asp.net gridview


【解决方案1】:

您需要在 GridView1_RowDataBound 事件中检查 RowType

试试这个

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Width = 100;
            e.Row.Cells[1].Width = 150;
        }
}

【讨论】:

  • 有效!谢谢!但是,如何在每个单元格上设置边界?我可以让它在所有网格或每一行上,但不是单元格
【解决方案2】:

尝试将 GridView AutoGenerateColumns 属性设置为 True 喜欢

AutoGenerateColumns="true"

编辑:

如果你检查的是 MSDN 的 DataAdapter.Fill 方法;您正在使用的重载不存在。 请看这里,http://msdn.microsoft.com/en-us/library/system.data.common.dataadapter.fill%28v=vs.80%29.aspx

用于填充 DataTable 的重载是 DataAdapter.Fill (DataTable, IDataReader)

您应该这样做,而不是通过创建 DataSet

DataSet ds = new DataSet();
dataAdapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

【讨论】:

  • 我使用 AutoGenerateColumns="true"
  • 设置断点并检查数据集ds。看看它是否被正确填充。从您的 html 设计器中发布与 GridView1 相关的标记代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
  • 2011-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多