如何选择datagridview的任何列的最大值和最小值 [英] How to select max and min value of any column of datagridview

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

问题描述

我在Windows窗体上有一个datagridview控件. 现在我需要选择一列的最大值和最小值. 在数据表中,我们可以使用此

I have a datagridview control on my windows form. Now i need to select max and min value of a column. In data-table we can do this by using this

int maxID = curriculmDataTable.AsEnumerable().Max(r => r.Field<int>("Id"));

如何在datagridview中实现这一目标.

How can i achieve this in datagridview.

推荐答案

您可以尝试:

var MaxID = dataGridView1.Rows.Cast<DataGridViewRow>()
                        .Max(r => Convert.ToInt32(r.Cells["Id"].Value));

确保您的Id单元格具有int类型值,否则请使用Int.TryParse,例如:

Make sure your Id cell has int type value, otherwise use Int.TryParse like:

int temp;
var MaxID2 = dataGridView1.Rows.Cast<DataGridViewRow>()
            .Max(r => int.TryParse(r.Cells["Id"].Value.ToString(), out temp) ? 
                       temp : 0 );

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

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