Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net Datagridview,如何在一定范围内获取最小值、最大值和平均值_Vb.net_Datagridview - Fatal编程技术网

Vb.net Datagridview,如何在一定范围内获取最小值、最大值和平均值

Vb.net Datagridview,如何在一定范围内获取最小值、最大值和平均值,vb.net,datagridview,Vb.net,Datagridview,我有一个DataGridView,它包含1000个数据点。我已经成功地计算了DataGridView中的最小值、最大值和平均值,但现在我只想从某个范围获得最小值、最大值和平均值。例如,101至200或201至300中的数据 For i As Integer = 0 To dataGridView1.Rows.Count() - 1 Step +1 sum_tmp = sum_tmp + dataGridView1.Rows(i).Cells(2).Value If i = 0

我有一个DataGridView,它包含1000个数据点。我已经成功地计算了DataGridView中的最小值、最大值和平均值,但现在我只想从某个范围获得最小值、最大值和平均值。例如,101至200或201至300中的数据

For i As Integer = 0 To dataGridView1.Rows.Count() - 1 Step +1

    sum_tmp = sum_tmp + dataGridView1.Rows(i).Cells(2).Value

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

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

    If min_tmp > dataGridView1.Rows(i).Cells(2).Value Then
        min_tmp = dataGridView1.Rows(i).Cells(2).Value
    End If

Next

avg_tmp = sum_tmp / dataGridView1.Rows.Count()
对于dataGridView1.Rows.Count()-1步骤+1,i As Integer=0的

sum_tmp=sum_tmp+dataGridView1.行(i).单元格(2).值
如果i=0,那么
max_tmp=dataGridView1.Rows(i).Cells(2).Value
min_tmp=dataGridView1.Rows(i).Cells(2).Value
如果结束
如果max_tmpdataGridView1.Rows(i).Cells(2).Value,则
min_tmp=dataGridView1.Rows(i).Cells(2).Value
如果结束
下一个
avg_tmp=sum_tmp/dataGridView1.Rows.Count()

上面是我获取值的代码。我曾尝试使用通过选择combobox更改的整数变量,但始终在min上得到0值。遗憾的是,它平均显示了正确的值。这怎么可能是错误的?

您可以使用LINQ表达式获取要处理的值的可枚举集合,然后使用内置函数获取最大值、最小值和平均值

定义将保存结果计算的结构

Private Structure ResultsStruct
    Public Min As Single
    Public Max As Single
    Public Average As Single
    Public Sum As Single
End Structure
此函数在调用时返回结构。传入第一行索引和最后一行索引

Private Function getResults(firstRow As Integer, lastRow As Integer) As ResultsStruct
    Dim skipRows = firstRow - 1
    Dim takeRows = lastRow - skipRows
    Dim values = DataGridView1.Rows.
        OfType(Of DataGridViewRow)().
        Select(Of Single)(Function(r) Single.Parse(r.Cells(0).Value.ToString())).
        Skip(skipRows).
        Take(takeRows)
    Dim max = values.Max()
    Dim min = values.Min()
    Dim avg = values.Average()
    Dim result As New ResultsStruct() With {.Max = values.Max(),
                                            .Min = values.Min(),
                                            .Average = values.Average(),
                                            .Sum = values.Sum()}
    Return result
End Function
怎么叫

Dim result = getResults(firstRow:=201, lastRow:=300)
MessageBox.Show(String.Format("Max: {0}, Min: {1}, Avg: {2}, Sum: {3}",
                              result.Max, result.Min, result.Average, result.Sum))

将示例中的
201
300
替换为您感兴趣的范围,无论是用户选择网格还是其他输入方式。

您可以使用LINQ表达式获取要处理的值的可枚举集合,然后使用内置函数获取最大值、最小值和平均值

定义将保存结果计算的结构

Private Structure ResultsStruct
    Public Min As Single
    Public Max As Single
    Public Average As Single
    Public Sum As Single
End Structure
此函数在调用时返回结构。传入第一行索引和最后一行索引

Private Function getResults(firstRow As Integer, lastRow As Integer) As ResultsStruct
    Dim skipRows = firstRow - 1
    Dim takeRows = lastRow - skipRows
    Dim values = DataGridView1.Rows.
        OfType(Of DataGridViewRow)().
        Select(Of Single)(Function(r) Single.Parse(r.Cells(0).Value.ToString())).
        Skip(skipRows).
        Take(takeRows)
    Dim max = values.Max()
    Dim min = values.Min()
    Dim avg = values.Average()
    Dim result As New ResultsStruct() With {.Max = values.Max(),
                                            .Min = values.Min(),
                                            .Average = values.Average(),
                                            .Sum = values.Sum()}
    Return result
End Function
怎么叫

Dim result = getResults(firstRow:=201, lastRow:=300)
MessageBox.Show(String.Format("Max: {0}, Min: {1}, Avg: {2}, Sum: {3}",
                              result.Max, result.Min, result.Average, result.Sum))

将示例中的
201
300
替换为您感兴趣的范围,无论是来自用户选择网格还是其他一些输入方式。

您是否可以使用
过滤功能
中间方法
使用
数据视图
获取值?在DataTable.FIlter()方法上进行google搜索`何时计算总和或平均值?用户是否选择一些行并单击上下文菜单项?@AlexB。你提出了一个好问题。我编辑了我的答案以简化函数的调用方式,输入了两个单一的值(第一行,最后一行)。您可以使用
过滤函数
中间方法
来使用
数据视图
获取值吗?在DataTable.FIlter()方法上进行google搜索`何时计算总和或平均值?用户是否选择一些行并单击上下文菜单项?@AlexB。你提出了一个好问题。我编辑了我的答案以简化函数的调用方式,需要输入两个值(第一行,最后一行)。谢谢你的答案,现在它解决了我的“基础知识不足”:D谢谢你的答案,现在它解决了我的“基础知识不足”:D