Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView DateTime column and DBNull

Status
Not open for further replies.

dalchri

Programmer
Apr 19, 2002
608
0
0
US
Hello,

I have a DataGridViewTextBoxColumn that is bound to a DateTime column which allows nulls. I can't figure out for the life of me how to allow the user to delete the text out of this column and get the DataGridView to successfully parse the text back to DBNull.

I thought it would be as simple as handling the CellParsing event:

Code:
private void grdEnrollment_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
	if (e.Value == "") {
		e.Value = DBNull.Value;
		e.ParsingApplied = true;
	}
}

But the DataGridView throws the DataError dialog up.

How are you supposed to do this?
 
The DataError dialog continues to be displayed with the following code:

Code:
private void grdSchedule_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
	if ((string) e.Value == "")
	{
		grdSchedule[e.ColumnIndex, e.RowIndex].Value = DBNull.Value;
		e.ParsingApplied = true;
	}
}

and

Code:
private void grdSchedule_CellParsing(object sender, DataGridViewCellParsingEventArgs e)
{
	if ((string) e.Value == "")
	{
		grdSchedule[e.ColumnIndex, e.RowIndex].Value = null;
		e.ParsingApplied = true;
	}
}

There seems to be no way to take over the assignment of the cell value and no acceptable way to tell the DataGridView to assign a null value to a DateTime column.

Am I missing something here?
 
You now have 2 options.

1. See if there's a method called SetDate_PubNull() available from your DataRow
2. Define your type as DateTime?

The ? allows the value to be null. By default in .Net you cannot have a null DateTime by design.

 
The solution that I have stumbled upon is that you can set the column's NullValue at form startup to an empty string. Apparently there is no way to do this through the IDE because a blank NullValue is interpreted as null by the form designer.

I'd still rather have the ability to do this through the CellParsing event. Although, come to think of it, I can probably turn whatever string values that I want interpreted as DBNull into the blank string which will then be turned into DBNull by the DataGridView later.

Code:
colDate.DefaultCellStyle.NullValue = "";
 
And it is even more subtle than that. Depending on how you tickle the IDE, sometimes it generates the following code:

Code:
// 
// colDate
// 
this.colDate.DataPropertyName = "Date";
dataGridViewCellStyle2.Format = "d";
dataGridViewCellStyle2.NullValue = null;
this.colDate.DefaultCellStyle = dataGridViewCellStyle2;
this.colDate.HeaderText = "Date";
this.colDate.Name = "colDate";
this.colDate.Width = 67;

Then, other times it will generate the following

Code:
// 
// colDate
// 
this.colDate.DataPropertyName = "Date";
dataGridViewCellStyle2.Format = "d";
this.colDate.DefaultCellStyle = dataGridViewCellStyle2;
this.colDate.HeaderText = "Date";
this.colDate.Name = "colDate";
this.colDate.Width = 67;

Notice how the line dataGridViewCellStyle2.NullValue = null; disappeared? That means the difference between a blank string being parsed to DBNull and not.
 
Based on what I am seeing in the .NET Reflector for DataGridView.PushFormattedValue, there is absolutely no way to perform DBNull assignment using the CellParsing event.

Also, to elaborate on the subtlety of the IDE behavior, you will only encounter this problem that I did if you change the format string for the DataGridViewColumn. At this point, the IDE generates the code dataGridViewCellStyle2.NullValue = null; Otherwise, the code is not generated and blank strings parse successfully to DBNull.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top