替换DataGridView中的DateTime.MinValue
我正在处理名称记录应用程序,信息存储在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的编码。
你只需要将它转换为DateTime
if ((DateTime)(resultsGrid.CurrentRow.Cells["DateOfBirth"].Value) == DateTime.MinValue) { // Set cell value to "" }
我发现了为什么DateTime.MinValue
正在显示!
单元格格式化部分中的这一行导致MinValue为“01/01/0001”以显示:
dateOfBirth.DefaultCellStyle.Format = "MM/dd/yyyy";
如果没有此行,我的datagridview
的出生日期字段将留空。
Barbaros Alp对于我当时尝试做的事情仍然是正确的。 感谢大家!
虽然实现所需的转换可能非常简单(请参阅我之前的优秀答案),但我觉得理想情况下应该使用NULL
替换数据库中此特定列中的所有DateTime.MinValue值。 这将正确地表示该职位中实际缺乏数据。 目前,您的数据库包含不正确的数据,并且您正在尝试对数据进行后处理以供显示。
因此,您可以使用EmptyDataText
或NullDisplayText
属性处理GridView中的数据显示,以提供适当的渲染。
使用nullabe DateTime格式而不是DateTime(“DateTime?”) – 不要使用字符串。
例:
public DateTime? mydate;
然后:
DGview.Rows.Add(mydate.HasValue ? mydate : null);
if (yourDateOfBirth != null) { Convert.ToDate(yourDateOfBirth) == DateTime.MinValue }
您可以将对象强制转换为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
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进行比较,但听起来您有正确的想法。
已经有答案可以达到你想要的效果; 但我想知道为什么你不在数据库中放入NULL开始? 在sqlite3中,“datetime”或多或少只是一个字符串,(日期时间列没有什么特别之处,因为sqlite3只是类型afinity而不是类型绑定),所以你只需在该列中插入NULL。 如果您想在查询时获得日期时间的MinValue,则可以轻松执行类似的查询
select coalesce(your_datetime_column,'01/01/0001') from your_table
- 混淆或保护.Net程序集的最佳方法
- System.Delegate System.Reflection.MethodInfo.CreateDelegate(System.Type)
- DataGridView AllowUserToAddRow属性不起作用
- 使用ItemsSource时,操作无效。 使用ItemsControl.ItemsSource访问和修改元素
- CloudBlockBlob.DownloadToStream与DownloadRangeToStream
- MSMQ接收和删除
- 鼠标滚轮在winforms中冒泡?
- 使用C#中的参数调用PowerShell脚本
- 多态自定义模型绑定器没有填充模型w /值