我有一个DataGridView
,其中一行包含日期,我想从该行中找到最小和最大值。
我怎么才能得到它呢?
发布于 2019-03-21 06:30:28
您可以使用集合的帮助:
// 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();
当然,更有效的方法是手动查找max,这样就不需要额外的收集了:)
发布于 2019-03-21 07:01:08
在我看来,使用linq方法。因为简短和基本的代码。
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();
https://stackoverflow.com/questions/55274815
复制