Hiding a Row in a GridView

To hide an entire row in a GridView control consists of two steps.  First, you need to override the OnRowCreated event of the GridView.  This event has a GridViewEventArgs parameter that will give you access to the underlying Row.DataItem, enabling you to show or hide a row based on a data value. (I tried this in the OnRowDataBound event with no luck, and this was the only other place I could find where DataItem wasn't null).

The second step which took some experimentation is to call the Clear() method on the Row's control's collection to prevent binding errors.

protected override void OnRowCreated( GridViewRowEventArgs e )
{
    base.OnRowCreated( e );
    if ( e.Row.RowType == DataControlRowType.DataRow )
    {
        if ( ( ( IMutableDto ) e.Row.DataItem ).IsDeleted )
            {
                e.Row.Visible = false;
                    e.Row.Controls.Clear();
                }
            }
        }

Posted 01-22-2007 2:29 AM by Ben Scott
Copyright © 2005-2007 Schmurgon Pty Ltd
Powered by Community Server (Non-Commercial Edition), by Telligent Systems