0

Imagine we have a grid such as:

Column1       Column2 ....
A             432423
A             344
A             5
B             45
B             77
C             65
D             4
D             545
.             .
.             .
.             .

I want to paint these rows based on the value on Column1,we don't know the values(so conditional formatting is not possible) they could be anything,but i just want to group them by color,for example I want the rows that have A in Column1 to be pink,then rows with B yellow then with C pink again and D yellow and so on.What kinda iteration could i use?

4

2 回答 2

1

我会使用RowDataBound

private Object lastValue = null;
private Color lastColor = Color.Yellow;
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRowView row = (DataRowView)e.Row.DataItem;
        Object thisValue = row["Column1"];
        if(thisValue == lastValue)
            e.Row.BackColor = lastColor;
        else
            e.Row.BackColor = lastColor == Color.Yellow ? Color.Pink : Color.Yellow;
        lastValue = thisValue;
        lastColor = e.Row.BackColor;
    }
}
于 2013-07-08T07:47:27.260 回答
0

好吧,一些类型细节和框架信息会很好,但作为一个纯算法:

for(int i = 0;i<columns[0].Count;i++)
{
Colour colour = Utils.GetColourFromValue(columns[0][i].Value);
for(int j = 0;i<columns.Count;j++)
{
columns[j][i].Colour = colour;
}
}
于 2013-07-08T07:46:14.017 回答