Click here to Skip to main content
15,885,032 members
Articles / Web Development / ASP.NET

GridThemes

Rate me:
Please Sign up or sign in to vote.
4.92/5 (51 votes)
4 Mar 2007CPOL10 min read 168.3K   1.5K   161  
Combining a BuildProvider and IExtenderProvider to create a declarative framework for conditional formatting in ASP.NET GridViews.
<%@ Page Language="C#" MasterPageFile="~/SampleGridThemes.master"%>

<%@ Register Assembly="GridThemes" Namespace="UNLV.IAP.GridThemes" TagPrefix="cc1" %>

<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="CreateCategoryDataSource"
        TypeName="DataProvider">
        <SelectParameters>
            <asp:Parameter DefaultValue="5" Name="numValueColumns" Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>
    <asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" GridLines="None">
    </asp:GridView>
    <br />

    This example builds on the previous two, testing <code>GroupText</code>
    to highlight the East Region.
                
<pre>  &lt;Theme id="Group6" title="Grouping: Group variables 3"&gt;
    &lt;Group column="0" suppressRepeating="true"&gt;

      &lt;If test='GroupText=="East Region"' &gt;
        &lt;If test='CellIndex==0'&gt;
          &lt;Apply BackColor='LightPink' /&gt;
          &lt;Else&gt;
            &lt;If test='RowIndexWithinGroup % 2 == 0'&gt;
              &lt;Apply BackColor='LightPink' /&gt;
              &lt;ElseApply BackColor='Pink' /&gt;
            &lt;/If&gt;
          &lt;/Else&gt;
        &lt;/If&gt;
      &lt;/If&gt;

      &lt;AlternateFormat&gt;
        &lt;If test='CellIndex==0'&gt;
          &lt;Apply BackColor='SkyBlue' /&gt;
          &lt;Else&gt;
            &lt;If test='RowIndexWithinGroup % 2 == 0'&gt;
              &lt;Apply BackColor='SkyBlue' /&gt;
              &lt;ElseApply BackColor='LightBlue' /&gt;
            &lt;/If&gt;
          &lt;/Else&gt;
        &lt;/If&gt;
      &lt;/AlternateFormat&gt;

      &lt;AlternateFormat&gt;
        &lt;If test='CellIndex==0'&gt;
          &lt;Apply BackColor='LightGreen' /&gt;
          &lt;Else&gt;
            &lt;If test='RowIndexWithinGroup % 2 == 0'&gt;
              &lt;Apply BackColor='LightGreen' /&gt;
              &lt;ElseApply BackColor='PaleGreen' /&gt;
            &lt;/If&gt;
          &lt;/Else&gt;
        &lt;/If&gt;
      &lt;/AlternateFormat&gt;

      &lt;if test="CellIndex==0 &amp;&amp; RowIndexWithinGroup==0"&gt;
        &lt;apply Font-Bold="true" 
               TextExpression='string.Format("{0}. {1}", GroupIndex + 1, GroupText)' 
               /&gt;
      &lt;/if&gt;

      &lt;if test="IsNumeric"&gt;
        &lt;apply horizontalAlign="right" /&gt;
      &lt;/if&gt;

    &lt;/Group&gt;

    &lt;Header&gt;
      &lt;Apply backColor="#CCCCCC" /&gt;
    &lt;/Header&gt;
  &lt;/Theme&gt;
</pre>            
    <br />
    <br />
    <cc1:gridthemesextender id="GridThemesExtender1" runat="server">
        <Props>
            <cc1:ExtenderProperties GridID="GridView1" GridTheme="Grouping: Group variables 3" />
        </Props>
    </cc1:gridthemesextender>
    
</asp:Content>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
University of Nevada, Las Vegas
United States United States
With a background in education, music, application development, institutional research, data governance, and business intelligence, I work for the University of Nevada, Las Vegas helping to derive useful information from institutional data. It's an old picture, but one of my favorites.

Comments and Discussions