【发布时间】:2012-07-24 09:02:38
【问题描述】:
我有一个绑定的gridview。
即使有重复,我也想在最长的交货时间内更改字体的颜色。我不知道如何编写我的 if 语句。
这是我想做的一个粗略的想法,尽管我知道这段代码是错误的。
if Max(LeadTime) Then
GridView.ForeColor = Color.Red
谁能帮助我?
【问题讨论】:
我有一个绑定的gridview。
即使有重复,我也想在最长的交货时间内更改字体的颜色。我不知道如何编写我的 if 语句。
这是我想做的一个粗略的想法,尽管我知道这段代码是错误的。
if Max(LeadTime) Then
GridView.ForeColor = Color.Red
谁能帮助我?
【问题讨论】:
您首先需要从数据源中获取最大值。你可以用 linq 做到这一点:
maxLeadTime = ds.Max(dsi => dsi.LeadTime)
在您的项目数据绑定事件处理程序中,将绑定的项目与最大值进行比较:
if (item.LeadTime == maxLeadTime)
{
/* do stuff */
}
【讨论】:
(VB.NET 版本)假设您将 Grid 绑定到数据表,您将这样做。
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)上也是一样的
在页面加载中:
maxVal = urList.Max(Function(x) x.LeadTime)
In Row dataBound:
Dim uc As urClass = e.Row.DataItem
If uc.LeadTime = maxVal Then
e.Row.BackColor = Drawing.Color.Red
End If
【讨论】: