Shuffle row / column colors in GridView

Suppose you have a GridView with multiple columns, for example:

| Foo | Bar | Total |

and you use a stylesheet so that the alternating lines are different from each other, for example, light blue and white.

Is there a good way to make a single column alternate in a different color? For example, I might want the Total column to alternate between medium and light red to draw attention to it in a large grid.

By the way, I know that you can programmatically change the color of a cell. However, I would like to stick with CSS if this is all possible, so all my style stuff is in one place. I also don’t see an easy way to determine if I get into a variable string when I'm inside an event handler.

+3
source share
4 answers

If you use jQuery, you can do this quite easily.

$("table#myTable col:odd").css("background-color:#ffe");

The selector is :oddnot available in most modern browsers, but jQuery provides it to us today.

For strings, you can do this with the built-in AlternatingRowStyle element.

Edit: found a good resource for some ways: http://css-discuss.incutio.com/?page=StylingColumns

+4
source

Besides Ben's suggestion , Matt Berseth also has a nice demonstration of how to highlight columns using GridViewControlExtender, which is pretty nice:

http://mattberseth2.com/demo/Default.aspx?Name=GridViewControlExtender+II+-+Header+Cell+MouseOver+Styles+and+a+Few+More+Live+Examples&Filter=All

, GridView :

http://mattberseth.com/blog/gridview/

Ajax Ajax Control Toolkit ASP.NET, jQuery.

0

2 ...

P.S. " , , ".

Row.RowState == RowState.Alternating

, CssClass ASP.NET, CSS.

0

WPF ListView GridView. StyleSelector , Bea Costa:

public class ListViewItemStyleSelector : StyleSelector
{
    private int i = 0;
    public override Style SelectStyle(object item, DependencyObject container)
    {
        // makes sure the first item always gets the first style, even when restyling
        ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(container);
        if (item == ic.Items[0])
        {
            i = 0;
        }
        string styleKey;
        if (i % 2 == 0)
        {
            styleKey = "ListViewItemStyle1β€³;
        }
        else
        {
            styleKey = "ListViewItemStyle2β€³;
        }
        i++;
        return (Style)(ic.FindResource(styleKey));
    }
}

, , , .

One thing that can help is that this only works for strings. It seems that columns always need to use CellTemplate/ Style.

0
source

All Articles