Sherlene Tan
Sherlene Tan

Reputation: 77

how to highlight the biggest value in a Gridview

I have a gridview that is binded.

And i want to change the color of the font for the longest leadtime even if there are duplicates. I have no idea on how to write my if statement.

This is a rough idea of what i want to do, though I know this code is wrong.

if Max(LeadTime) Then

GridView.ForeColor = Color.Red

Can anyone help me?

Upvotes: 2

Views: 891

Answers (2)

CodingSlayer
CodingSlayer

Reputation: 915

(VB.NET version) Assuming you are binding you Grid to a datatable, this is how you will do it.

 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

it would be the same if you are binding it to a list(of T)

In PAge Load:

 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

Upvotes: 0

Paul Fleming
Paul Fleming

Reputation: 24526

You first need to get the max value from your datasource. You can do this with linq:

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

In your item data bound event handler, compare the bound item with the max value:

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

Upvotes: 3

Related Questions