Search This Blog

Dynamically highlighting rows/cells in a GridView

posted on Thursday, March 22, 2012


Highlight rows/cells in the GridView that have a certain value (in the database). Easy, right? True, but depending on the situation there could be multiple ways to implement this, as I discovered today :) So, below you'll find three options on how to highlight specific rows/cells in a GridView.

#Option 1: best solution for BoundFields

When you're working with bound fields, this is probably the best solution. In the RowDataBound-event, find and check the specific value and apply highlighting using e.Row or e.Row.Cells.
protected void gvPersonen_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // this gridview shows 'Persoon'-objects, so you can get the object through the EventArgs
                Persoon p = (Persoon)e.Row.DataItem;

                // check the specific value
                if (p.Naam.Equals("Veerle"))
                {
                    e.Row.Cells[0].ForeColor = Color.Blue;
                }
            }
        }
<asp:gridview autogeneratecolumns="False" id="gvPersonen" onrowdatabound="gvPersonen_RowDataBound" runat="server">
     <columns>
         <asp:boundfield datafield="Naam" headertext="Naam"></asp:boundfield>
         ...
     </columns>
</asp:gridview>

#Option 2: one solution for TemplateFields

When you're working with template fields that contain one or more controls, this is the way to go. In the RowDataBound-event, find and check the specific value and apply highlighting using e.Row or e.Row.Cells and the FindControl-method.
protected void gvPersonen_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // this gridview shows 'Persoon'-objects, so you can get the object through the EventArgs
                Persoon p = (Persoon)e.Row.DataItem;

                // find the Label-control
                Label lblNaam = e.Row.Cells[0].FindControl("lblNaam") as Label;

                // check the specific value
                if (p.Naam.Equals("Veerle"))
                {
                    lblNaam.Visible = true;
                } else {
                    lblNaam.Visible = false;
                }
            }
        }
<asp:gridview autogeneratecolumns="False" id="gvPersonen" onrowdatabound="gvPersonen_RowDataBound" runat="server">
     <columns>
         <asp:templatefield>
            <itemtemplate>
               <div id="divPersoon" runat="server">
<asp:label id="lblNaam" runat="server" text="&lt;%# Eval(&quot;Naam&quot;) %&gt; "></asp:label>
                  <asp:label id="lblVoornaam" runat="server" text="&lt;%# Eval(&quot;Voornaam&quot;) %&gt;"></asp:label>
               </div>
</itemtemplate>
         
         ...
     </asp:templatefield></columns>
</asp:gridview>

#Option 3: another solution for TemplateFields

When you're working with template fields that contain specific values, lay-out or mark-up, you can use databinding-methods. Call the method from within your column to determine the content of the column or the content of a property of the column. In this case it's used to determine true or false for the "visible"-property. You can use it to fill the "text"-property as well, amongst others. Some properties won't work, but you'll be alarmed by a very flashy error saying it's not a databinding method.
protected bool ShowValue(object o)
        {
            // get the "persoon"-object using the Id passed from the gridview (stored as DataKeyNames)
            DTO.Login use = new DTO.Login();
            LoginDA userDa = new LoginDA();
            user = userDa.GetPersoonById(o.ToString());

            // check the specific value
            if (user.Name.Equals("Veerle"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
<asp:gridview autogeneratecolumns="False" id="gvPersonen" onrowdatabound="gvPersonen_RowDataBound" runat="server">
     <columns>
         <asp:templatefield>
            <itemtemplate>
               <div id="divPersoon" runat="server">
<asp:label id="lblTest" runat="server" visible="&lt;%# ShowValue(Eval(&quot;Id&quot;)) %&gt;">
                  </asp:label>
               </div>
</itemtemplate>
         </asp:templatefield>
         ...
     </columns>
</asp:gridview>

So, as you can see, nothing really special about it but I just wanted to summarize SOME of the different possibilities when highlighting rows/cells in a GridView. It was helpfull for me, so it might be for you.

Could be useful, right?

No comments:

Post a Comment