在一个DataGridView替换DateTime.MinValue [英] Replacing a DateTime.MinValue in a DataGridView
问题描述
我的工作的名称记录应用程序和信息存储在SQLite数据库。数据库中的所有列TEXT类型,除了出生的列,这是一个DATETIME的日期。原来的Access数据库,我转移到SQLite数据库允许为空值的出生日期,所以当我复制过来,我都设置为空值DateTime.MinValue。
I am working on a name record application and the information is stored in a SQLite database. All columns in the database are TEXT types, except for the date of birth column, which is a DATETIME. The original Access database that I transferred to the SQLite database allowed nulls for the date of birth, so when I copied it over, I set all nulls to DateTime.MinValue.
在我的应用程序,出生列的日期格式,像这样:
In my application, the date of birth column is formatted like so:
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。
My problem is that rows where there is not a date of birth, the database has DateTime.MinValue, which displays in my DataGridView as 01/01/0001.
我要寻找一种方式用一个空字符串(),以代替01/01/0001我的DataGridView。
I am looking for a way to replace the 01/01/0001 with an empty string ("") in my DataGridView.
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? !谢谢
Anyone have an idea how I can replace a DateTime.MinValue in my DataGridView with an empty string? Thanks!
编辑:铸造用日期时间单元格的值允许if语句我有工作,但我还是没能弄清楚编码与更换MinValues一个空字符串。
Casting the cell value with DateTime allows the if statement I have to work, but I am still not able to figure out the coding to replace the MinValues with a blank string.
推荐答案
您只需将它转换为DateTime
You just need to cast it to DateTime
if ((DateTime)(resultsGrid.CurrentRow.Cells["DateOfBirth"].Value) == DateTime.MinValue)
{
// Set cell value to ""
}
这篇关于在一个DataGridView替换DateTime.MinValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!