简体   繁体   中英

Find the largest integer in Datagridview column Vb.net

I want to know how to compares values in a column to get the largest integer in the specific column. Considering Column (0) has integers in it how to find the largest integer?

i tried the coding below it dosent work

           Dim abc As Integer = Datagrid.RowCount - 1
           Dim abcd As Integer = Datagrid.Rows(abc).Cells(0).Value
           MsgBox(abcd)

IF the column (0) load from accending then it will ofcourse get the largest when the user sorts the column or any column it simply gets the last rows cell (0) value. Is there a way to loop through and get the largest integer? The msgbox is just to let me know what is the number.

Try this

Dim abcd as Integer
For x As Integer = 0 to Datagrid.Rows.Count - 1
   If abcd = 0 then
      abcd = Datagrid.Rows(x).Cells(0).Value
   Else
      if abcd < Datagrid.Rows(x).Cells(0).Value Then abcd = Datagrid.Rows(x).Cells(0).Value 
   Endif
Next
MsgBox(abcd)

Something like this should do it (more pseudocode than anything):

function findLargestInColumn(DataGridView dgv, int colNum)
{
    int maxVal = dgv.Rows(0).Cells(colNum).Value
    for (int i = 1 to dgv.Rows.Count)
        maxVal = ( dgv.Rows(i).Cells(colNum).Value > maxVal ? dgv.Rows(0).Cells(colNum).Value : maxVal )

    return maxVal
}

Of course, you could easily adapt this to be in-line if you don't want to make a function for it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM