ASP:DataGrid显示两次列

时间:2014-07-02 19:45:17

标签: c# asp.net datagrid

我希望在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应解决问题,但它没有做任何事情。

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

解决方案: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 

祝福