【问题标题】:how to hide datatable column from gridview in asp.net如何在asp.net的gridview中隐藏数据表列
【发布时间】:2014-09-06 12:17:26
【问题描述】:

我想在 gridview 中隐藏列。我创建一个数据表并从数据库中添加数据,然后将其与 gridview 绑定。现在我想从它隐藏 0 索引列,但它没有隐藏。 请帮帮我。

这是我的代码。

`int companyID = Convert.ToInt16(Session["companyID"]);

            DataTable taskTable = new DataTable("TaskList");

            SqlConnection sql_connection = new SqlConnection("data source=.\\SQLEXPRESS;Integrated Security=SSPI; initial catalog=db_ease");
            SqlCommand sqlCommand = new SqlCommand("select tbl_employee.empID,tbl_employee.empEmploymentID,tbl_employee.empFirstname,tbl_employee.empMiddlename,tbl_employee.empLastname,tbl_employee.empGender,tbl_employee.empMobileNo,tbl_employee.empMac,tbl_association.associationStatus from tbl_employee inner join tbl_association on tbl_employee.empID=tbl_association.empID where tbl_association.companyID="+companyID, sql_connection);
            SqlDataAdapter adapter = new SqlDataAdapter(sqlCommand);

            adapter.Fill(taskTable);
            Session["TaskTable"] = taskTable;

            taskTable.Columns[0].ColumnName = "Employee ID";
            taskTable.Columns[1].ColumnName = "Employment ID";
            taskTable.Columns[2].ColumnName = "First Name";
            taskTable.Columns[3].ColumnName = "Middle Name";
            taskTable.Columns[4].ColumnName = "Last Name";
            taskTable.Columns[5].ColumnName = "Gender";
            taskTable.Columns[6].ColumnName = "Mobile No";
            taskTable.Columns[7].ColumnName = "MAC Address";
            taskTable.Columns[8].ColumnName = "Status";


            BindData();
            displayGridView.Columns[0].Visible = false;
            sql_connection.Close();`

这是我的网格视图

<asp:GridView ID="displayGridView" runat="server" CssClass="table table-striped table-bordered dataTable" OnRowEditing="displayGridView_RowEditing" OnRowUpdating="displayGridView_RowUpdating" OnRowDeleting="displayGridView_RowDeleting" OnRowCancelingEdit="displayGridView_RowCancelingEdit" AllowPaging="true" OnPageIndexChanging="displayGridView_PageIndexChanging" OnSorting="displayGridView_RowSorting" AutoGenerateDeleteButton="true" AutoGenerateEditButton="true" PagerSettings-FirstPageText="First" PagerSettings-LastPageText="Last" PagerSettings-Mode="NumericFirstLast" PagerSettings-PageButtonCount="4" PageSize="4" EmptyDataText="No data found" AllowSorting="True" OnDataBound="displayGridView_OnDataBound" > </asp:GridView>

【问题讨论】:

标签: asp.net gridview datatable


【解决方案1】:

当您只需要 gridview 中的某些列时,您应该只在 aspx 文件中添加这些列,如下所示:

   <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server">
    <Columns>
        <asp:BoundField HeaderText="Header Name" DataField="NameOfFieldInDatabase" />
        <asp:BoundField HeaderText="Header Name2" DataField="NameOfFieldInDatabase2" />
        <asp:BoundField HeaderText="Header Name3" DataField="NameOfFieldInDatabase3" />
    </Columns>
</asp:GridView>

您还应该像上面的代码一样将 AutoGenerateColumns="false" 添加到 gridview,否则您将拥有自定义列以及所有其他自动生成的列

【讨论】:

    【解决方案2】:

    你应该像这样隐藏 displayGridView_RowDataBound 事件上的列。

       protected void displayGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
    
            e.Row.Cells[0].Visible = false;
    
        }
    

    【讨论】:

    • OnDataBound="GridView_RowDataBound" 这个事件会用到吗?
    • 指定的参数超出了有效值的范围。参数名称:索引“这是我添加此代码时出现的错误”
    • if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[0].Visible = false;试试这个.....
    • 以及如何隐藏此列标题?
    猜你喜欢
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 2011-07-17
    • 2015-04-05
    • 2014-12-16
    • 1970-01-01
    • 2016-12-06
    • 2013-02-05
    相关资源
    最近更新 更多