stonypaul
stonypaul

Reputation: 677

Maximum value in a gridview column

I have a gridview which is databound to a sql datasource and displays data as it should but now I want to display the highest value in a column in a textbox. I have assigned the column to a literal called litPathwayDays. The column displays a cumulative total as an Integer and obviously the last value in the column will be the highest. This is the shell I have for the databound event and I am working with VB.net 2.0. I'm thinking I need a For loop to iterate over all the values in the column? Could someone fill in the gaps please. Thanks.

Protected Sub GridRootCause_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridRootCause.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim lit As Literal = e.Row.FindControl("litPathwayDays") 

        //For Each................

    End If
End Sub

Upvotes: 1

Views: 2921

Answers (2)

ZL1Corvette
ZL1Corvette

Reputation: 415

If you have a DataTable then:

Dim Max as Double

For Each row As DataRow In dt.Rows
        If row.Item("<ColumnName>") > Max Then
                row.Item("<ColumnName>") = Max
        End If
Next

Or if you have a DataGridView:

Dim Max as Double

For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Item("<ColumnName>") > Max Then
                row.Item("<ColumnName>") = Max
        End If
Next

Also, it kind of looks like you don't want the max from the column since it would be a sum of the column value so:

Dim Sum, Max as Double

For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Item("<ColumnName>") > Sum Then
                Max = Sum
                row.Item("<ColumnName>") = Sum
        End If
Next

Use Max.

Upvotes: 1

xspydr
xspydr

Reputation: 3060

[data grid name].Columns[[data grid name].Columns.Length]...

Upvotes: 0

Related Questions