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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

allow null date in dataGridView 1

Status
Not open for further replies.

EDB2

MIS
Sep 11, 2002
36
US
I'm having a heck of a time trying to get this to work, but it has to be possible! I have a bound DataGridView that includes a bunch of date columns, which may or may not be null (the database does allow nulls in these fields). Null values are displayed with no problem, but I want the user to be able to edit these fields and delete a date if someone put the date in the wrong field. However as soon as I leave the cell (after erasing the date) I get the error "System.FormatException: String was not recognised as a valid DateTime".

IsDBNull(cell value) does return true, so it recognizes that the cell is null. How can I get the grid to accept a null without an error?



 
Ed82,

I'm guessing that you have formatting set for the date column, when you don't enter a valid date and try and leave the cell you will get this error. It's a pain, I know. There are 2 solutions I can think of.

1, Remove the formatting from the column

2, Assign a "" as a null value in the form.designer such as DataGridViewCellDateCell1.NullValue = ""

The easiest way do do this is from the DataGridView select Edit Columns, then expand the "Default Cell Style" property to open the "CellStyle Builder." From there you can enter a NullValue, the problem is if you enter "" the builder will think you don't want to enter a null value. What I do is enter something random like "findme." then close the builder. Next from the solution explorer select the "ShowAllFiles" button at the top and expand the form.vb and open the form.Designer.vb file. when it's open simply search for "findme" and you will find something like

DataGridViewCellDateCell1.NullValue = "findme"
simply change it to
DataGridViewCellDateCell1.NullValue = ""

Save the file and things should work the way you want.


 
I'd never have figured that out in a million years (grin). Thank you so much! It worked like a charm!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top