VB.NET - How To Get The Maximum Value From DataGridView Column Using VB NET


vb datagridview column max value

VB.NET - How To Get The Value Maximum In DataGridView Column Using VB NET

                                                                                                                         

In This VB.NET Tutorial  We Will See How To Get The Maximum Value From DataGridView  Column And Display It In TextBox Using VBNET Programming Language.


Project Source Code:

Public Class VB_Datagridview_Column_Cells_Max_Value

    Private Sub VB_Datagridview_Column_Cells_Max_Value_Load(sender As Object, e As EventArgs) Handles MyBase.Load

       ' populate datagridview with some data
        Dim rand As New Random()
        For i As Integer = 0 To 11 Step +1
            dataGridView1.Rows.Add("First Name" + i.ToString(), "Last Name" + i.ToString(), rand.Next(20, 65).ToString())
        Next
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        dataGridView1.AllowUserToAddRows = False

        ' Method 1
        textBoxMax.Text = (From row As DataGridViewRow In dataGridView1.Rows
        Where row.Cells(2).FormattedValue.ToString() <> String.Empty
                        Select Convert.ToInt32(row.Cells(2).FormattedValue)).Max().ToString()

        ' Method 2
        Dim max As Integer
        For i As Integer = 0 To dataGridView1.Rows.Count() - 1 Step 1

            If i = 0 Then
                max = dataGridView1.Rows(i).Cells(2).Value
            End If

            If max < dataGridView1.Rows(i).Cells(2).Value Then
                max = dataGridView1.Rows(i).Cells(2).Value
            End If

        Next
        textBoxMax.Text = max.ToString()
    End Sub
End Class
      
///////////////OUTPUT:

vb.net datagridview max column value
vb datagridview column max value





Share this

Related Posts

Previous
Next Post »