簡體   English   中英

如何從asp.net中的GridView隱藏數據表列

[英]how to hide datatable column from gridview in asp.net

我想在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();`

這是我的gridview

<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>

您應該像這樣在displayGridView_RowDataBound事件上隱藏列。

   protected void displayGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        e.Row.Cells[0].Visible = false;

    }

當您只需要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,否則您將擁有自定義列以及所有其他自動生成的列

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM