【问题标题】:Replacing a DateTime.MinValue in a DataGridView替换 DataGridView 中的 DateTime.MinValue
【发布时间】:2010-10-08 18:47:51
【问题描述】:

我正在开发一个姓名记录应用程序,信息存储在 SQLite 数据库中。数据库中的所有列都是 TEXT 类型,但出生日期列除外,它是 DATETIME。我转移到 SQLite 数据库的原始 Access 数据库允许出生日期为空,所以当我复制它时,我将所有空设置为 DateTime.MinValue。

在我的应用程序中,出生日期列的格式如下:

DataGridViewTextBoxColumn dateOfBirth = new DataGridViewTextBoxColumn();
        dateOfBirth.HeaderText = "DOB";
        dateOfBirth.DataPropertyName = "DateOfBirth";
        dateOfBirth.Width = 75;
        dateOfBirth.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
        dateOfBirth.DefaultCellStyle.Format = "MM/dd/yyyy";

我的问题是没有出生日期的行,数据库有 DateTime.MinValue,在我的 DataGridView 中显示为 01/01/0001。

我正在寻找一种在我的 DataGridView 中用空字符串 ("") 替换 01/01/0001 的方法。

private void resultsGrid_DateFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
    {
        if(resultsGrid.Columns[e.ColumnIndex].Name.Equals("DateOfBirth"))
        {
            if ((DateTime)(resultsGrid.CurrentRow.Cells["DateOfBirth"].Value) == DateTime.MinValue)
            {
                 // Set cell value to ""
            }
        }
    }

有人知道如何用空字符串替换 DataGridView 中的 DateTime.MinValue 吗?谢谢!

编辑:使用 DateTime 转换单元格值允许我必须使用 if 语句,但我仍然无法弄清楚用空白字符串替换 MinValues 的编码。

【问题讨论】:

标签: c# datetime sqlite datagridview


【解决方案1】:

您只需要将其转换为 DateTime

if ((DateTime)(resultsGrid.CurrentRow.Cells["DateOfBirth"].Value) == DateTime.MinValue)
            {
                 // Set cell value to ""
            }

【讨论】:

  • 我在上面的代码中添加了您的更改,它确实允许我的 if 语句工作,但我仍然无法计算出实际更改单元格内容的代码。
  • 在 if 语句中放置 // 将单元格值设置为 "";我猜你需要写这段代码 resultsGrid.CurrentRow.Cells["DateOfBirth"].Value = "" 它应该这样做
  • resultsGrid.CurrentRow.Cells["DateOfBirth"].Value = ""
【解决方案2】:

我发现了为什么会显示 DateTime.MinValue

单元格格式部分中的这一行导致“01/01/0001”的 MinValue 显示:

dateOfBirth.DefaultCellStyle.Format = "MM/dd/yyyy";

如果没有这一行,我的datagridview 中的“出生日期”字段将留空。

Barbaros Alp 关于我当时尝试做的事情仍然是正确的。谢谢大家!

【讨论】:

    【解决方案3】:

    使用 nullabe DateTime 格式而不是 DateTime ("DateTime?") - 不要使用字符串。

    示例:

    public DateTime? mydate;
    

    然后:

    DGview.Rows.Add(mydate.HasValue ? mydate : null);
    

    【讨论】:

      【解决方案4】:

      虽然实现您需要的转换可能非常简单(请参阅我之前的出色答案),但我认为您最好将数据库中此特定列中的所有 DateTime.MinValue 值替换为NULL。这将正确地表示该位置实际缺少数据。目前您的数据库包含不正确的数据,您正在尝试对数据进行后处理以供显示。

      因此,您将能够使用 EmptyDataTextNullDisplayText 属性来处理 GridView 中的数据显示,以提供适当的呈现。

      【讨论】:

      • 我是使用 SQLite 的新手,所以我可能错了,但我认为 DateTime 不能为空。如果可以,那将是最佳解决方案,因为您是对的 - 我实际上将不正确的数据放入了我的数据库中。
      • 不幸的是,我没有使用过 SQLite,而且知道的比你还少。不过,我认为这值得研究。看看文档。他们的网站告诉我,在 SQLite 2 中,一切都是无类型的。对于 SQLite3 - sqlite.org/datatype3.html
      【解决方案5】:
      if (yourDateOfBirth != null)
      {
          Convert.ToDate(yourDateOfBirth) == DateTime.MinValue
      }
      

      【讨论】:

        【解决方案6】:

        您可以将对象转换为 DateTime 对象:

        //create a nullable intermediate variable
        System.DateTime? dtCellValue = resultsGrid.CurrentRow.Cells["DateOfBirth"].Value as System.DateTime?;
        
        //if this cell has been set to String.Emtpy the cast will fail
        //so ensure the intermediate variable isn't null
        if ( dtCellValue.HasValue && dtCellValue == System.DateTime.MinValue )
        //set cell value appropriately
        

        【讨论】:

          【解决方案7】:
          private void resultsGrid_DateFormatting(object sender, System.Windows.Forms.DataGridViewCellFormattingEventArgs e)
          {
              if(resultsGrid.Columns[e.ColumnIndex].Name.Equals("DateOfBirth"))
          {
              if ((string)resultsGrid.CurrentRow.Cells["DateOfBirth"].Value == DateTime.MinValue.ToString())
              {
                   // Set cell value to ""
              }
          }
          

          }

          或者您可能必须转换为 DateTime,然后与 MinValue 进行比较,但听起来您的想法是正确的。

          【讨论】:

            【解决方案8】:

            已经有答案可以实现您想要的;但我想知道你为什么不把 NULL 放在数据库中?在 sqlite3 中,“datetime”或多或少只是一个字符串,(datetime 列没有什么特别之处,因为 sqlite3 只是类型关联,而不是类型绑定),所以你只需在该列中插入 NULL。如果您想在查询时获得日期时间的 MinValue,您可以轻松执行类似

            的查询
            select coalesce(your_datetime_column,'01/01/0001') from your_table
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-08-04
              • 1970-01-01
              相关资源
              最近更新 更多