skip to Main Content

For context read on but, ultimately, is it possible/(and how) to update (remove/hide a column) a GridView in the OnDataBound() or OnPreRender() events?

I query data from a database and display it on a gridview. I’m using the OnRowDataBound event to format numbers, etc. In that result set I have a column named ‘orderby’ that I use to order the result rows. I would like to remove this column from the gridview. If I autogeneratecolumns I can hide this column in the OnRowDataBound() event by doing the following (e.Row.Cells[r].Visible = false;) on the DataControlRowType.Header and DataControlRowType.DataRow. If I don’t autogenerate columns and I add hidden="true" in the column parameters of the .aspx, it does not work. I’m not looking for a solution in the .aspx btw…

For some reason I thought that the OnDataBound() and OnPreRender() events could be used to update GridViews (hide rows, columns, create total row) but that does not seem to be the case. If I try the below it does not work (gridView.Columns.Count is 0 even though there is data). I can get the desired effect if I use the OnRowDataBound() event but I was wondering:

  1. can the OnDataBound() and OnPreRender() events update a gridview? If so pls point me to an example.
  2. what can you do with the OnDataBound() and OnPreRender()/what are they good for? I haven’t really used them before and, again, I thought that they would be able to help with last minute updates/touchups on grid views.
    Note: The OnRowDataBound() method is doing updates row by row, column by
    column. With a complicated page you can easily have some nasty long
    code. So I would imagine that once all of that is sorted out there
    would be a way to ‘easily’ hide a column for example; but I could be
    wrong…

btw I’m running: .Net version: 4.0.30319.42000, runtimeVer: .NET Framework 4.8.4645.0

    protected void checks_DataBound(object sender, EventArgs e)
    {
        GridView gridView = (GridView)sender;
        if (gridView.ClientID == "checks_db_sod01")
        {
            for (int c = 0; c < gridView.Columns.Count; c++)
            {
                if (gridView.Columns[c].HeaderText.ToLower().Trim() == "orderby")
                {
                    gridView.Columns[c].Visible = false;
                }
            }
        }
    }

3

Answers


  1. Chosen as BEST ANSWER

    SOLUTION Thanks to Ortund and Rand Random I tried a few things. To be clear, Ortund's reply used gridView.Columns and for some reason, in OnPreRender my gridView had 3 rows and 0 columns. Without OnPreRender() the gridView whould display 4 rows (1 header, 3 data rows) and 3 columns.

    What finally worked is posted below. Note that the solution is iterating through header rows and columns (O(N)) which is OK I guess. Also, if I don't explicitly hide the corresponding headerCell I still get the column but without data. If anyone has another solution, hopefully a faster/more efficient one, that would be most appreciated! Thank you again for those who responded. If anyone has any resource on how to properly do cool things with GridViews pls post it. I'm usually hacking away updates/changes in the OnRowDataBound() method only...

        protected void checks_PreRender01(object sender, EventArgs e)
        {
            var gridView = (GridView)sender;
            if (gridView.ClientID == "checks_db_px01")
            {
                int targetColumnIndex = -1;
                if (gridView.HeaderRow != null)
                {
                    foreach (TableCell headerCell in gridView.HeaderRow.Cells)
                    {
                        if (headerCell.Text.ToLower().Trim() == "orderby")
                        {
                            headerCell.Visible = false;
                            targetColumnIndex = gridView.HeaderRow.Cells.GetCellIndex(headerCell);
                            break;
                        }
                    }
                }
                if (targetColumnIndex != -1)
                {
                    foreach (GridViewRow row in gridView.Rows)
                    {
                        row.Cells[targetColumnIndex].Visible = false;
                    }
                }
    
            }
        }
    

  2. You should probably stick to one question per post but, to address your specific queries:

    1. Yes, OnDataBound() and OnPreRender() can update certain aspects of the GridView such as data formatting or applying styles. They’re not used for structural changes like removing columns; for that, you’d typically manipulate the GridView’s properties directly or handle such tasks during the GridView’s initialization or data binding phases.
    2. These events are for used for formatting, applying styles, calculating totals, etc. Think of it as post-processing.

    You could possibly use PreRender to hide your column though. Note that it’s been about 5 years since I used web forms, but this might get you started:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Initialize or bind your GridView here.
            // You can manipulate GridView columns before or after binding.
        }
    }
    
    protected void checks_DataBound(object sender, EventArgs e)
    {
        // You can perform final adjustments to the GridView after data binding.
        // This could include formatting, styling, calculations...
    }
    
    protected void checks_PreRender(object sender, EventArgs e)
    {
        var gridView = (GridView)sender;
        if (gridView.ClientID == "checks_db_sod01")
        {
            // Iterate through columns and hide the one with header text "orderby".
            foreach (DataControlField column in gridView.Columns)
            {
                if (column.HeaderText.ToLower().Trim() == "orderby")
                {
                    column.Visible = false;
                }
            }
        }
    }
    

    Hope this helps!

    Login or Signup to reply.
  3. You could use the RowCreated Event of the GridView and some LINQ to achieve this

    protected void checks_RowCreated(object sender, GridViewRowEventArgs e)
    {
        GridView gridView = (GridView)sender;
        if (gridView.ClientID == "checks_db_sod01")
        {
            ((DataControlField)gridView.Columns
                .Cast<DataControlField>()
                .Where(fld => fld.HeaderText.ToLower().Trim() == "orderby")
                .SingleOrDefault()).Visible = false;
        }
    }
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search