简体   繁体   English

在Datagridview列Vb.net中找到最大的整数

[英]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? 考虑到列(0)中包含整数,如何找到最大整数?

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. 如果列(0)的负载增加了,那么当用户对列或任何列进行排序时,它当然将获得最大负载,它仅获得最后一行的单元格(0)值。 Is there a way to loop through and get the largest integer? 有没有办法遍历并获取最大整数? The msgbox is just to let me know what is the number. msgbox只是让我知道电话号码是多少。

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. 当然,如果您不想为其创建函数,则可以轻松地使其内联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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