C# 显示datagridview的最近日期
我有一个datagridview,其中有一列填充了日期,我想要一个文本框只显示最近的一个,我所做的是在文本框(01/01/0000)中放置一个非常早的日期,然后在for循环中运行整个datagrid,但它给了我这个错误 字符串未被识别为有效的日期时间C# 显示datagridview的最近日期,c#,datetime,datagridview,C#,Datetime,Datagridview,我有一个datagridview,其中有一列填充了日期,我想要一个文本框只显示最近的一个,我所做的是在文本框(01/01/0000)中放置一个非常早的日期,然后在for循环中运行整个datagrid,但它给了我这个错误 字符串未被识别为有效的日期时间 dataGridView1.Columns[14].DefaultCellStyle.Format = "dd/MM/yyyy"; for (int i = 0; i < dataGridView1.Rows.Count;
dataGridView1.Columns[14].DefaultCellStyle.Format = "dd/MM/yyyy";
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DateTime date = Convert.ToDateTime(dataGridView1.Rows[i].Cells[14].Value.ToString());
DateTime date1 = DateTime.ParseExact(dataRecente.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);
if (date > date1)
{
dataRecente.Text = Convert.ToString(date);
}
}
dataGridView1.Columns[14].DefaultCellStyle.Format=“dd/MM/yyyy”;
对于(int i=0;i日期1)
{
dataRecente.Text=Convert.ToString(日期);
}
}
为什么不直接从datagridview
的源代码中获取日期,这可能是一个数据表
或数据集
?如果基础数据稍后发生更改(例如由于用户输入),datatable会触发一个事件,因此您可以刷新文本框
循环是完全不必要的,解析和转换也是如此。如果您有有效的日期时间值,则可以轻松对其进行排序
此外,您假设日期总是在某一列中,但用户可以拖动列并更改显示顺序,您的代码将严重崩溃
因此,不是:
dataGridView1.Columns[14].DefaultCellStyle.Format = "dd/MM/yyyy";
你可以有:
dataGridView1.Columns[datagridview1.Columns["colDate"].Index].DefaultCellStyle.Format = "dd/MM/yyyy";
(未测试,但如果不正确,应非常接近)
datagrid中的每一列都是一个控件,希望您已经给了它们有意义的列名(在本例中:colDate
)
您可以使用以下方法从数据表中获取最大日期:
(请务必处理datatable为空的情况,或期望返回值为null)
其他选择:使用数据表。选择函数或LINQ为什么不直接从datagridview
的源代码获取日期,可能是数据表
或数据集
?如果基础数据稍后发生更改(例如由于用户输入),datatable会触发一个事件,因此您可以刷新文本框
循环是完全不必要的,解析和转换也是如此。如果您有有效的日期时间值,则可以轻松对其进行排序
此外,您假设日期总是在某一列中,但用户可以拖动列并更改显示顺序,您的代码将严重崩溃
因此,不是:
dataGridView1.Columns[14].DefaultCellStyle.Format = "dd/MM/yyyy";
你可以有:
dataGridView1.Columns[datagridview1.Columns["colDate"].Index].DefaultCellStyle.Format = "dd/MM/yyyy";
(未测试,但如果不正确,应非常接近)
datagrid中的每一列都是一个控件,希望您已经给了它们有意义的列名(在本例中:colDate
)
您可以使用以下方法从数据表中获取最大日期:
(请务必处理datatable为空的情况,或期望返回值为null)
其他选择:使用数据表。选择函数或LINQ我发现了另一个…难以置信的野蛮和不卓越的解决方案,因为我不关心界面外观,作为一个noob,我对数据集知之甚少,等等,我创建了三个数据网格,一列数天,一列数月,一列数年。
然后,脚本按年份排序,位于同一年份顶部的脚本进入第二个datagridview并按月份排序,脚本在当天重复,最后最后一个datagris的第一行包含最近的日期。
正如所说,这是难以置信的野蛮,似乎是尼安德特人的解决方案…但它奏效了
for (int i2 = 0; i2 < date.Rows.Count; i2++)
{
int inizio = Int32.Parse(date.Rows[i2].Cells[1].Value.ToString());
int fine = Int32.Parse(date.Rows[i2].Cells[2].Value.ToString());
for (int i = inizio; i < fine; i++)
{
dataSortingGrid.Rows.Add();
int startIndex = 0;
int length = 2;
string dataIntera = dataGridView1.Rows[i].Cells[14].Value.ToString();
int i3 = dataSortingGrid.Rows.Count - 1;
String giorni = dataIntera.Substring(startIndex, length);
dataSortingGrid.Rows[i3].Cells[0].Value = giorni;
startIndex = 3;
length = 2;
String mesi = dataIntera.Substring(startIndex, length);
dataSortingGrid.Rows[i3].Cells[1].Value = mesi;
startIndex = 6;
length = 4;
String anni = dataIntera.Substring(startIndex, length);
dataSortingGrid.Rows[i3].Cells[2].Value = anni;
dataSortingGrid.Rows[i3].Cells[3].Value = dataGridView1.Rows[i].Cells[14].Value.ToString();
}
dataSortingGrid.Sort(dataSortingGrid.Columns[2], System.ComponentModel.ListSortDirection.Descending);
//mesi
int nRowMesi = Int32.Parse(dataSortingGrid.Rows.Count.ToString());
for (int i = 0; i < nRowMesi; i++)
{
if (i < 1)
{
dataSortingGridMesi.Rows.Add();
dataSortingGridMesi.Rows[i].Cells[0].Value = dataSortingGrid.Rows[i].Cells[0].Value;
dataSortingGridMesi.Rows[i].Cells[1].Value = dataSortingGrid.Rows[i].Cells[1].Value;
dataSortingGridMesi.Rows[i].Cells[2].Value = dataSortingGrid.Rows[i].Cells[2].Value;
dataSortingGridMesi.Rows[i].Cells[3].Value = dataSortingGrid.Rows[i].Cells[3].Value;
}
else
{
int valoreAnniSopra = Convert.ToInt32(dataSortingGrid.Rows[i - 1].Cells[2].Value);
int valoreAnniSotto = Convert.ToInt32(dataSortingGrid.Rows[i].Cells[2].Value);
if (valoreAnniSotto < valoreAnniSopra)
{
break;
}
dataSortingGridMesi.Rows.Add();
dataSortingGridMesi.Rows[i].Cells[0].Value = dataSortingGrid.Rows[i].Cells[0].Value;
dataSortingGridMesi.Rows[i].Cells[1].Value = dataSortingGrid.Rows[i].Cells[1].Value;
dataSortingGridMesi.Rows[i].Cells[2].Value = dataSortingGrid.Rows[i].Cells[2].Value;
dataSortingGridMesi.Rows[i].Cells[3].Value = dataSortingGrid.Rows[i].Cells[3].Value;
}
}
dataSortingGridMesi.Sort(dataSortingGridMesi.Columns[1], System.ComponentModel.ListSortDirection.Ascending);
//giorni
int nRowGiorni = Int32.Parse(dataSortingGridMesi.Rows.Count.ToString());
for (int i = 0; i < nRowGiorni; i++)
{
if (i < 1)
{
dataSortingGridGiorni.Rows.Add();
dataSortingGridGiorni.Rows[i].Cells[0].Value = dataSortingGridMesi.Rows[i].Cells[0].Value;
dataSortingGridGiorni.Rows[i].Cells[1].Value = dataSortingGridMesi.Rows[i].Cells[1].Value;
dataSortingGridGiorni.Rows[i].Cells[2].Value = dataSortingGridMesi.Rows[i].Cells[2].Value;
dataSortingGridGiorni.Rows[i].Cells[3].Value = dataSortingGridMesi.Rows[i].Cells[3].Value;
}
else
{
int valoreMesiSopra = Convert.ToInt32(dataSortingGridMesi.Rows[i - 1].Cells[1].Value);
int valoreMesiSotto = Convert.ToInt32(dataSortingGridMesi.Rows[i].Cells[1].Value);
if (valoreMesiSotto < valoreMesiSopra)
{
break;
}
dataSortingGridGiorni.Rows.Add();
dataSortingGridGiorni.Rows[i].Cells[0].Value = dataSortingGridMesi.Rows[i].Cells[0].Value;
dataSortingGridGiorni.Rows[i].Cells[1].Value = dataSortingGridMesi.Rows[i].Cells[1].Value;
dataSortingGridGiorni.Rows[i].Cells[2].Value = dataSortingGridMesi.Rows[i].Cells[2].Value;
dataSortingGridGiorni.Rows[i].Cells[3].Value = dataSortingGridMesi.Rows[i].Cells[3].Value;
}
}
dataSortingGridGiorni.Sort(dataSortingGridGiorni.Columns[0], System.ComponentModel.ListSortDirection.Descending);
for(int i2=0;i2