开发者

ASP.net Gridview highlight max values in multiple columns

I have a gridview that shows, for example, a baseball team's statistics. It's a standard sports stats grid - the rows show statistics for each player and the columns show the specific stat for the each player.

Easy enough so far. But what I would then like to do is style (highlight or bold) the Max or Min (the team leader) of each stat column. For example, Player A may only lead in one or two categories, so we cannot style the entire row. If Player A just leads the team in strikeouts, I just want to style the number of strikeouts he had (THAT cell only).

What's the best way to handle this? Make SQL Server do all the work and in effect, rank EVERY stat of each player, effectively doubling the number of columns (e.g. col: AB, col: ABRank). Or do I let the rowdatabound event of the gridview handle this?

If I chose the latter, I think I would get the Max of 开发者_如何学JAVAevery statistical category from the datatable before binding (e.g. store them in a local variable) then on rowdatabound, if they match the value, apply the style.


You already gave the answer to your own question, which is by the way the answer I'd have given you.

Or do I let the rowdatabound event of the gridview handle this?

If I chose the latter, I think I would get the Max of every statistical category from the datatable before binding (e.g. store them in a local variable) then on rowdatabound, if they match the value, apply the style.


There is an option you didn't mention. You could use javascript clientside to do the highlighting. should be rather simple to run over a table am highlight the highest values in a column.


Depending on what you are doing you probably have a certain amount of columns you bind to.

I would use LINQ on your DataSource, before you bind it, for each type you want the max for example:

// create a global variable to hold the data
int _maxHomeRuns = 0;

// Then before you bind the datasource, find out the max of each stat
_maxHomeRuns = baseballStats.Max(i => i.HomeRuns);  // get the max

// Then in your template columns Label control DataBinding method    
if ((int)(Eval("HomeRuns")) == _maxHomeRuns)
{
    // Assign the style you want
    ((Label)(sender)).CssClass = "MaxCellStyle";
}

I wouldn't use the RowDataBinding, do it at the control level's OnDataBinding so you are scoping the checks specifically to a control so you don't have to search for controls on a row.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜