2012-07-24 65 views
2

我有一个绑定的gridview。如何突出Gridview中的最大值

而且即使存在重复,我也想更改最长提前期的字体颜色。我不知道如何写我的if语句。

这是我想要做的粗略想法,虽然我知道这段代码是错误的。

if Max(LeadTime) Then 

GridView.ForeColor = Color.Red 

任何人都可以帮我吗?

回答

3

您首先需要从数据源获取最大值。你可以使用LINQ做到这一点:

maxLeadTime = ds.Max(dsi => dsi.LeadTime) 

在绑定的事件处理程序的项目数据,与最大值比较绑定项:

if (item.LeadTime == maxLeadTime) 
{ 
    /* do stuff */ 
} 
+0

对不起

Private maxVal As Decimal Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Me.Page.IsPostBack Then dim dt as datatable = GetTable() maxVal = ds.AsEnumerable.Max(Function(dr) dr("lead_time")) gv.DataSource = dt gv.DataBind() End If End Sub Private Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then Dim dr As DataRowView = e.Row.DataItem If dr("lead_time") = maxVal Then e.Row.BackColor = Drawing.Color.Red End If End If End Sub 

如果你绑定到(的T)

列表,网页载入这将是相同的我可以知道什么是dsi? – 2012-07-24 09:14:40

+0

dsi是传递到[lambda表达式](http://msdn.microsoft.com/zh-cn/library/bb397687.aspx)的项目的别名。 – 2012-07-24 09:55:18

0

(VB.NET版)假设你是结合你网格到一个数据表,这是你将如何做到这一点。

maxVal = urList.Max(Function(x) x.LeadTime) 

行数据绑定:

Dim uc As urClass = e.Row.DataItem 
     If uc.LeadTime = maxVal Then 
      e.Row.BackColor = Drawing.Color.Red 
     End If 
相关问题