Display Alternate Syle for Alternating Rows in C#

May

20

by Mike Fleming at 7:44 am (1 Comment) .NET |


This is just a quick little tip to help you display alternating row colors in your C# code for a DataList or GridView control. Using the AlternatingItemTemplate method is a pain, as it forces you to duplicate your entire ItemTemplate block just to add the alternating CSS class. While trying to find another means of accomplishing this I found the Container.ItemIndex syntax. Using this you can write a little if statement with a MOD function in it to have the code display your alternating style. So my DataList now looks like this:


">


Title ID Edit Delete
"> "> ">Edit Record


So every other row will now use a class named trDark. To use this with a GridView you would use the syntax of Container.DisplayIndex.


Categories .NET | Tags:

One Response to “Display Alternate Syle for Alternating Rows in C#”

  1. His_wife14

    October 22, 2009
    1:41 pm

Leave a Reply or Return to Top