[英]How to find out Minimum & maximum Date from a datagridview in C#?
我有DataGridView
,其中一行包含日期,我想從該行中找到最小值和最大值。
我怎么才能得到它?
您可以使用集合的幫助:
// 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();
當然,更有效的方法是手動查找最大值,然后你不需要額外的收集來為你做:)
在我的估計中,使用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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.