简体   繁体   English

如何从C#中的datagridview中找出最小和最大日期?

[英]How to find out Minimum & maximum Date from a datagridview in C#?

I have DataGridView in which a row conatins dates and I want to find min & max value from that row. 我有DataGridView ,其中一行包含日期,我想从该行中找到最小值和最大值。

How can i get it? 我怎么才能得到它?

You could use help of collections: 您可以使用集合的帮助:

// select row with index that you want (I used 0 for example)
var row = dataGridView1.Rows[0];
List<DateTime> datesInRow = new List<DateTime>();
foreach (DataGridViewCell cell in row.Cells)
  datesInRow.Add((DateTime)cell.Value);
var maxDate = datesInRow.Max();

Of course, more efficient way would be finding max manually, then you wouldn't need extra collection to do it for you :) 当然,更有效的方法是手动查找最大值,然后你不需要额外的收集来为你做:)

in my estimation, using linq methods. 在我的估计中,使用linq方法。 Because short and basic code. 因为简短和基本的代码。

var dateTimes = dataGridView1.Rows.Cast<DataGridViewRow>()
            //.Select(x => (DateTime) x.Cells["ColumnName"].Value); //if column type datetime
            //or    
            .Select(x => Convert.ToDateTime(x.Cells["ColumnName"].Value));

var minValue = dateTimes.Min();
var maxValue = dateTimes.Max();

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

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