Conditional Footnotes On GridView

hand-079 A couple of days ago I ran into a requirement to conditionally display footnote information at the bottom of a gridview display.  We were already conditionally displaying icons in the row.  What we needed to do was to conditionally show text at the bottom of the screen that explained what each icon was for.  Here’s how we went about it.

The first thing we did was to make all of the footnotes invisible.  We will only make them visible if the associated icon is visible.  So we need some way of determining if the icon is visible, and if it is we need to make the associated footnote visible.

First, we find out if the icon is visible.

There is an event that fires every time a row is created in the gridview control.  The event name is RowCreated.  This event sends the sender object and a GridViewRowEventArgs object down to the handler.  And this is where the magic starts.

There are two ways of finding a control in the row.  You can go after it using:

e.Row.Cells[cellControlIsIn].Controls[indexOfTheControl];

Which you can then cast to the type of control you are expecting and check the Visible property to see if it is visible.

The other thing you can do is recursively look for the control by its ID by iterating through the Controls collection of each control in the Row.

We’ve written a utility function that does all the work:

public static Control
    FindControlByID(string id,
    ControlCollection controls)
{
    Control returnControl = null;
    foreach (Control c in controls)
    {
        if (c.ID == id)
            return c;

        returnControl =
            FindControlByID(id, c.Controls);
        if (returnControl != null)
            return returnControl;
    }
    return returnControl;
}

If you want the absolute fastest code, you’ll use the first method.  If you just want to get the code working and out the door, you can use the second method.

Once we know that the control is visible, we can set the footnote visible in the event handler and we are done.

Related Post

Comments are closed.