locked
Hide Columns in c# DataGrid View RRS feed

  • Question

  • User-1901014284 posted

    Hi,

    I have a DataGrid view where there a columns that I require for calculations but do not want to display to the user on the front end within the DataGrid view. Is there any way I can hide fro example the ef.Work_Time_DurationHours column of the below?

    using (var connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
    {
    var query = "SELECT " +
    "ef.Expense_Date AS 'Expense Date', c.Client_Name AS 'Client Name', ef.Created_By AS 'Created By', l.Location, vco.Visit_Call_Other AS 'Vist, Call, Other', ef.Justification, TravelStartTimeStart AS 'Travel Start', TravelEndTimeStart, ef.Work_Start_Time AS 'Work Start Time', ef.Work_End_Time AS 'Work End Time', ef.Work_Time_DurationHours AS 'Work Time Hours', ef.Total_Mileage AS 'Total Mileage', ef.Meal_Cost AS 'Meal Cost', ef.Hotel_Cost AS 'Hotel Cost', ef.Parking_Road_Toll AS 'Parking/Road Toll', ef.Other_Cost AS 'Other Cost', ef.Other_Costs_Justification AS 'Other Cost Justification'" +
    "FROM " +
    "Expense_Entry_Form ef " +
    "LEFT JOIN Customer c ON (ef.Client_ID = c.ID) " +
    "LEFT JOIN Location l ON (ef.Location = l.ID) " +
    "LEFT JOIN Visit_Call_Other vco ON (ef.Visit_Call_Other = vco.ID) " +
    "WHERE Created_By = '" + Session["userName"].ToString() + "'" +
    "ORDER BY Expense_Date DESC";

    using (var command = new SqlCommand(query, connection))
    {
    var da = new SqlDataAdapter(command);
    var ds = new DataSet();
    da.Fill(ds);

    if (ds.Tables.Count > 0)
    {
    ExportExpDataGrid.DataSource = ds.Tables[0];
    ExportExpDataGrid.AllowPaging = true;
    ExportExpDataGrid.DataBind();
    }

    connection.Close();
    }

    Any help would be greatly appreciated.

    Many thanks

    Jonny

    Tuesday, August 1, 2017 12:11 PM

Answers

  • User2103319870 posted

    he only problem I have is that I do not use ASP to create the column headers all of the column headers are created within my string query. Is there a way I could get this to work from the server side?

    You can use OnItemDataBound Method of DataGrid. 

    Sample Code

    protected void Item_Bound(Object sender, DataGridItemEventArgs e)
            {
                if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
                {
                    //Here change the cell index as per your need
                    //index starts from 0 so if you want to hide first column add 0 in index
                    e.Item.Cells[2].CssClass = "hideGridColumn";
                }
                if (e.Item.ItemType == ListItemType.Header)
                {
                    //Here change the cell index as per your need
                    //index starts from 0 so if you want to hide first column add 0 in index
                    e.Item.Cells[2].CssClass = "hideGridColumn";
                }
    
            }

    And then attach the handler to DataGrid

       <style type="text/css">
                .hideGridColumn {
                    display: none;
                }
            </style>
            <asp:DataGrid ID="ItemsGrid" runat="server"
                BorderColor="black"
                BorderWidth="1"
                CellPadding="3"
                OnItemDataBound="Item_Bound"
                AutoGenerateColumns="true">
            </asp:DataGrid>

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 1, 2017 3:39 PM
  • User2103319870 posted

    Would this method also be available to use within a GridView

    Yes its available but the name of event is not ItemDataBound its RowDatabound event

     <asp:GridView runat="server" ID="GridView1" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
                    <Columns>
                       
                    </Columns>
                </asp:GridView>
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                  
                }
            }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 2, 2017 1:15 PM
  • User2103319870 posted

    the only part that is not hidden is the header of the rows I have hidden using the below code. Is there a way i can hide the header as well as the rows?

    You could try with the below code to hide header and cells in gridview

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[14].Visible = false;
                }
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    e.Row.Cells[14].Visible = false;
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 2, 2017 2:37 PM

All replies

  • User-718146471 posted

    that can be done programmatically like this:

    To hide a DataGridView column programmatically
    
    1.Double-click the column on the DataGridView control to add the default CellContentClick event handler, and then add the following code. This code hides the first column in the DataGridView control.
    
    this.customersDataGridView.Columns[0].Visible = false;
    
    2.Press F5 to run the code.
    
    3.Click inside the DataGridView control and verify that the Customer ID column is hidden.
    
    From: https://msdn.microsoft.com/en-us/library/bb383893(v=vs.90).aspx 
    
    

    Tuesday, August 1, 2017 1:45 PM
  • User-1901014284 posted

    Thank you very much for your response, I have tried this and received the below error message:

    Index was out of range. Must be non-negative and less than the size of the collection.

    Tuesday, August 1, 2017 2:42 PM
  • User2103319870 posted

    Index was out of range. Must be non-negative and less than the size of the collection.

    You have set the column Visible property to false. You wont be able to get the value for a control whose visible property set to false. The reason behind is, if DataGrid has any column for which visible property is set to "false" then that columns is not rendered at runtime in DataGrid and data of hidden column won’t be available .

    An easiest solution will be to hide the column using css styles instead of using the property.You can use css display property like given below

    Please use the below code

    First add the below css styles in page

    <style type="text/css">
            .hideGridColumn
            {
                display: none;
            }
        </style>

    Then use the css in your gridview column like given below

    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"
                    HeaderStyle-CssClass="hideGridColumn" ItemStyle-CssClass="hideGridColumn" />
    Tuesday, August 1, 2017 2:56 PM
  • User-1901014284 posted

    Thank you for your quick response, the only problem I have is that I do not use ASP to create the column headers all of the column headers are created within my string query. Is there a way I could get this to work from the server side?

    Tuesday, August 1, 2017 3:01 PM
  • User-718146471 posted

    Ok, can you post your complete code so we can see how you have this set up?

    Tuesday, August 1, 2017 3:27 PM
  • User2103319870 posted

    he only problem I have is that I do not use ASP to create the column headers all of the column headers are created within my string query. Is there a way I could get this to work from the server side?

    You can use OnItemDataBound Method of DataGrid. 

    Sample Code

    protected void Item_Bound(Object sender, DataGridItemEventArgs e)
            {
                if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
                {
                    //Here change the cell index as per your need
                    //index starts from 0 so if you want to hide first column add 0 in index
                    e.Item.Cells[2].CssClass = "hideGridColumn";
                }
                if (e.Item.ItemType == ListItemType.Header)
                {
                    //Here change the cell index as per your need
                    //index starts from 0 so if you want to hide first column add 0 in index
                    e.Item.Cells[2].CssClass = "hideGridColumn";
                }
    
            }

    And then attach the handler to DataGrid

       <style type="text/css">
                .hideGridColumn {
                    display: none;
                }
            </style>
            <asp:DataGrid ID="ItemsGrid" runat="server"
                BorderColor="black"
                BorderWidth="1"
                CellPadding="3"
                OnItemDataBound="Item_Bound"
                AutoGenerateColumns="true">
            </asp:DataGrid>

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 1, 2017 3:39 PM
  • User-1901014284 posted

    Thank you very much, this has worked perfectly. Would this method also be available to use within a GridView?

    Wednesday, August 2, 2017 1:04 PM
  • User2103319870 posted

    Would this method also be available to use within a GridView

    Yes its available but the name of event is not ItemDataBound its RowDatabound event

     <asp:GridView runat="server" ID="GridView1" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false">
                    <Columns>
                       
                    </Columns>
                </asp:GridView>
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                  
                }
            }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 2, 2017 1:15 PM
  • User-1901014284 posted

    Again thank you very much that has worked perfectly, the only part that is not hidden is the header of the rows I have hidden using the below code. Is there a way i can hide the header as well as the rows?

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

    Wednesday, August 2, 2017 1:29 PM
  • User2103319870 posted

    the only part that is not hidden is the header of the rows I have hidden using the below code. Is there a way i can hide the header as well as the rows?

    You could try with the below code to hide header and cells in gridview

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[14].Visible = false;
                }
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    e.Row.Cells[14].Visible = false;
                }
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 2, 2017 2:37 PM
  • User-1901014284 posted

    Perfect again! thank you very much.

    Wednesday, August 2, 2017 3:00 PM