0

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.

4

2 回答 2

3

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)
于 2013-06-18T03:27:15.753 回答
1

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.

于 2013-06-18T03:28:27.320 回答