【问题标题】:ASP:DataGrid displaying columns twiceASP:DataGrid 显示列两次
【发布时间】:2014-07-02 19:45:16
【问题描述】:

我想在我的DataGrid 对象中绑定列。在我添加它们之前,代码运行良好,但现在我得到了重复的列,特别是一列我根本不想要。这是 ASP:

        <ASP:DataGrid id="UserDataGrid" AutoGenerateColums="False" runat="server">
            <Columns>
                <asp:EditCommandColumn CancelText="Cancel" EditText="Edit" UpdateText="Update" HeaderText="Edit item">
                    <ItemStyle Wrap="False" />
                    <HeaderStyle Wrap="False"/>
                </asp:EditCommandColumn>
                <asp:BoundColumn DataField="Email" HeaderText="Email" ReadOnly="false" SortExpression="Email" />
                <asp:BoundColumn DataField="UserID" HeaderText="User ID" ReadOnly="false" SortExpression="UserID" />
                <asp:BoundColumn DataField="FullName" HeaderText="User Name" ReadOnly="false" SortExpression="FullName" />
            </Columns>
        </ASP:DataGrid>

以及背后的代码:

    DataTable dtUsers = new DataTable();
    dtUsers = dataAccessManager.ExecuteSQLForTable("SELECT * FROM tblUser");
    UserDataGrid.DataSource = dtUsers;
    UserDataGrid.DataBind();

我读过的所有内容都说将 AutoGenerateColumns 设置为 false 应该可以解决问题,但它没有做任何事情。

【问题讨论】:

    标签: c# asp.net datagrid


    【解决方案1】:

    查看您提供的代码,AutoGenerateColumns 似乎拼写错误。如果你修复它应该可以正常工作。

    <asp:DataGrid ID="UserDataGrid" AutoGenerateColumns="false" runat="server">
    

    【讨论】:

    • 这么多的脸。我以为VS2010会抱怨与类不匹配的属性?
    【解决方案2】:

    解决方案:1

    autogeneratecolumns 属性通常为 true,如果我们将 DataGrid 绑定到表格,DataGrid 将显示表格的所有列。只有当您将 autogeneratecolumns 设置为 false 时,您才可以让 DataGrid 显示您分配的列。

    但根据你的说法,这是行不通的。好的

    解决方案:2

    您可以隐藏 DataGrid 列

     DataTable dtUsers = new DataTable();
        dtUsers = dataAccessManager.ExecuteSQLForTable("SELECT * FROM tblUser");
        UserDataGrid.DataSource = dtUsers;
        UserDataGrid.DataBind();
        //Right Below line for hide columns
        UserDataGrid.Columns[0].Visible = false;//Hide First column of the DataGrid 
        UserDataGrid.Columns[1].Visible = false;//Hide Second column of the DataGrid 
        UserDataGrid.Columns[2].Visible = false;//Hide Third column of the DataGrid 
    

    解决方案:3

    您可以删除 DataGrid 列

     DataTable dtUsers = new DataTable();
        dtUsers = dataAccessManager.ExecuteSQLForTable("SELECT * FROM tblUser");
        UserDataGrid.DataSource = dtUsers;
        UserDataGrid.DataBind();
        //Right Below line for Remove columns
    
        UserDataGrid.Columns[0].Remove(); //Remove First column of the DataGrid 
        UserDataGrid.Columns[1].Remove(); //Remove Second column of the DataGrid 
        UserDataGrid.Columns[2].Remove(); //Remove Third column of the DataGrid 
    

    最好的祝福

    【讨论】:

      猜你喜欢
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 2012-05-19
      相关资源
      最近更新 更多