Search code examples
c#asp.netcssgridviewhidden-field

GridView Non-Visible Row Showing up in Empty Data Row


I have a custom GridView that will show 6 rows of data as a default (regardless of how many rows) with a pager at the bottom. This Gridview is used for numerous unique pages. On one page, I have a column that I set to hidden (width 0 and Css-Display=none) that I need -- another part of program depends on it being set that way.

The column is NOT visible in rows where there is data (as it shouldn't be). However, the column is showing up in the empty rows as an empty column (should not be visible). How would I go about making the EMPTY rows hide the COLUMN as well?


Solution

  • In my GridView_DataBound function, the cells were being generated for each column that existed -- how it filled in the empty cells. I added the if statement to check if the field was supposed to be hidden, and ensure the autogenerated cell was hidden as well.

    foreach (DataControlField field in _gridView.Columns)
    {
        TableCell cell = new TableCell();
        cell.Text = " ";
    
        if (field.ItemStyle.CssClass == "hideGridColumn")
            cell.Visible = false;
    
        row.Cells.Add(cell);
    }