0

I want to hide column in gridview. I create a datatable and add data in it from database then bind it with gridview. And now i want to hide 0 index column form it but it is not hiding. Please help me.

This is my Code.

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

This is my 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>

Muhammad Asfund
  • 97
  • 2
  • 10
  • possible duplicate of [How to hide a TemplateField column in a GridView](http://stackoverflow.com/questions/4954871/how-to-hide-a-templatefield-column-in-a-gridview) – naveen Jul 16 '14 at 11:07

2 Answers2

0

you should hide column on displayGridView_RowDataBound event like this.

   protected void displayGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {

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

    }
sp_m
  • 2,647
  • 8
  • 38
  • 62
0

When you only need some of the columns in your gridview you should add only that columns in your aspx file like below:

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

You also should add the AutoGenerateColumns="false" to gridview like the code above, otherwise you would have the customized columns plus all other auto generated columns

Nick Mehrdad Babaki
  • 11,560
  • 15
  • 45
  • 70