【问题标题】:show the most recent date of a datagridview显示 datagridview 的最近日期
【发布时间】:2020-03-10 16:32:03
【问题描述】:

我有一个数据网格视图,其中有一列填充了日期,我想要一个文本框只显示最近的一个,我所做的是在文本框 (01/01/0000) 中放置一个非常早的日期,然后运行整个数据网格都在一个 for 循环中,但它给了我这个错误

字符串未被识别为有效的日期时间

 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);
            }
        }

【问题讨论】:

  • 首先,DGV 是一个糟糕的数据容器选择。 DataTableList&lt;T&gt; 可以是允许使用一行代码进行过滤的数据源。接下来,没有理由在循环中一遍又一遍地解析日期 - 它是相同的日期。此外, DateTimePicker 将消除解析任何内容的需要。您也很有可能在循环后期遇到 NRE
  • 按日期降序排列,然后取第一个值。
  • 我正在订购该列,但它认为它是一个字符串,因此它按天而不是按日期排序,例如 01/01/1000 将比程序的 30/02/2017 更新

标签: c# datetime datagridview


【解决方案1】:

为什么不直接从datagridview 的源中获取日期,大概是datatabledataset?如果基础数据稍后发生变化,例如由于用户输入,数据表可以触发 事件,因此您可以刷新文本框。

循环是完全没有必要的,解析和转换也是如此。如果您有有效的日期时间值,则可以轻松对其进行排序。

此外,您假设日期将始终在某个列中,但用户可以拖动列并更改​​显示顺序,您的代码将严重崩溃。

所以而不是:

dataGridView1.Columns[14].DefaultCellStyle.Format = "dd/MM/yyyy";

你可以:

dataGridView1.Columns[datagridview1.Columns["colDate"].Index].DefaultCellStyle.Format = "dd/MM/yyyy";

(未测试,但如果不正确应该非常接近)

数据网格中的每一列都是一个控件,希望您为它们提供了有意义的列名(在本例中:colDate)。

您可以使用 DataTable.Compute function 从数据表中获取最大日期,例如:

table.Compute("MAX([DateColumn 1])", "");

(请处理数据表为空或期望返回值为空的情况)

其他替代方法:使用DataTable.Select 函数或LINQ

【讨论】:

  • datagridview 不是由用户编辑的,它是通过将 excel 粘贴到其中创建的,在这种情况下,日期将始终位于第 14 列,所以我如何将该列提取到数据表?
【解决方案2】:

我发现了另一个...令人难以置信的粗鲁和 unelagent 解决方案,因为我不关心界面的外观,而且作为菜鸟,我对数据集知之甚少等,我已经创建了三个数据网格,其中一列持续数天数月和一个多年。 然后脚本按年份排序,同一年份的顶部进入第二个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);

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多