0

我正在构建一个gridview,gridview 项目源包含索引值和其他数据。我知道我可以通过将IValueConverter背景与 index 属性绑定来应用背景,但我需要为此的逻辑。我想要如下所示的gridview。

在此处输入图像描述

4

2 回答 2

0
int i=0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (i == 0)
    {
        e.Row.Cells[0].BackColor = System.Drawing.Color.Purple;
        e.Row.Cells[1].BackColor = System.Drawing.Color.Green;
        e.Row.Cells[2].BackColor = System.Drawing.Color.Red;
        i++;
    }
    else if (i == 1)
    {
        e.Row.Cells[0].BackColor = System.Drawing.Color.Green;
        e.Row.Cells[1].BackColor = System.Drawing.Color.Red;
        e.Row.Cells[2].BackColor = System.Drawing.Color.Purple;
        i++;
    }
    else if (i == 2)
    {
        e.Row.Cells[0].BackColor = System.Drawing.Color.Red;
        e.Row.Cells[1].BackColor = System.Drawing.Color.Purple;
        e.Row.Cells[2].BackColor = System.Drawing.Color.Green;
        i = 0;
    }
}
于 2013-06-24T14:08:13.480 回答
0

终于找到了解决方案,我自己:)

public class IndexToColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        SolidColorBrush _color = null;
        int _val = (int)value % 9;
        switch (_val)
        {
            case 1:
            case 3:
            case 8:
                _color = App.Current.Resources["GreenBrush"] as SolidColorBrush;
                break;

            case 0:
            case 5:
            case 7:
                _color = App.Current.Resources["OrangeBrush"] as SolidColorBrush;
                break;

            case 2:
            case 4:
            case 6:
                _color = App.Current.Resources["PurpleBrush"] as SolidColorBrush;
                break;
        }

        return _color;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}
于 2013-06-25T05:48:13.130 回答