如何找到gridview列数据的最大值 [英] how to find max value of gridview column data

查看:207
本文介绍了如何找到gridview列数据的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有示例gridview数据如下
我在不使用数据库的情况下添加行

id名称sal

1 aaa 100

2 bbb 150

3 ccc 200

4 ddd 120



i have the sample gridview data is as follows
which i am adding rows without using a database

id name sal

1 aaa 100

2 bbb 150

3 ccc 200

4 ddd 120

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            int max_cols = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Columns"));

        }





need to write code for getting max value of the column


想知道列的最大值,即在上面的示例中sal 200的最大值

谢谢inadvance


want to know the maximum value of the column ,i.e in above example the max value of sal 200

thanks inadvance

推荐答案

排序? GridView是否绑定?在这种情况下,您可以复制和排序数据源并获得最高价值.
如果您希望在不对实际网格进行排序的情况下获得最大值,并且无法对行中每个数据源的排序,则可以获取sal的值,将其转换为整数并获取最大的数字.
在这里也可以选择使用LINQ.这样,您不必进行排序,但仍可以获取Max值.
在VB中(您未指定语言),例如:
Sorting? Is the GridView bound? In that case you could copy and sort the datasource and get the highest value.
If you wish to get the highest value without sorting the actual grid and you cannot sort a datasource you could for each through the rows, get the value of sal, cast it to an integer and retrieve the highest number.
Using LINQ might also be an option here. That way you do not have to sort, but you can still get the Max value.
In VB (you didn''t specify a language) something like:
Dim highestValue = DataGridView1.Rows.OfType(Of DataGridViewRow).Max(Function(row) Convert.ToInt32(row.Cells("sal").Value))


在C#中(从我的头顶开始):


In C# (from the top of my head):

int highestValue = DataGridView1.Rows.OfType<DataGridViewRow>.Max(row => Convert.ToInt32(row.Cells("sal").Value))


如果您的网格已绑定,那么它也可以直接应用于数据源.


This could also be applied directly to the datasource, if your grid is bound.


您的网格已绑定到数据源,因此需要查询最大值而不是网格.
Your grid is bound to a datasource so query it for a Max value, not the grid.


这篇关于如何找到gridview列数据的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆