I have a DataGridView which has a DataGridViewComboBoxColumn column named "Assigned To" which allows users to assign tasks to a user of the system. The DataGridView is databound to a DataTable called "Tasks" and the "Assign To" dropdown is databound to a table called "Employees". The linkage between the two tables is employee_id.
I would like the users to have the ability to un-assign a task. I am having problems setting the "Assigned To" value to null. I had two solutions to accomplish this:
1) Let the user hit "Ctrl+0" to null out the assign to value
2) Add a blank item in the Assign To dropdown that has a blank for the displayMember and a null for the valueMemeber
In either case, an exception is thrown that says:
"Cannot set Column "employee_id" to be null. Please use DBNull instead"
How do you make the DataGridViewComboBoxColumn except a null value
Thanks
Eric

DataGrid View - Ctl+0 to null out a combobox column
sethi.bhushan
Thanks for the reply...
I changed the column property AllowDbNull = true. This didn't help. I received the same exception.
Maybe some more background of what I am trying to do would help:
Tasks get entered into our task management system through our production processes. They are not entered manually in this application. When they get entered into the database, they are not assigned to anyone and have an employee_id = null.
Manager(s) would go through the un-assigned tasks and assign the appropriate resource.
The reason why I would need to un-assign the task, is in-case the manager makes a mistake and assigns a task to someone they shouldn't have. They need some way of un-assigning the task and putting it back in the pool of tasks so other managers can assign them to the appropriate employee.
BTW, the datagridview contains a lot more columns then just the employee the task is assigned, like task description, task id, due date, etc...
Also, in the application a user cannot delete tasks.
Hope this explains a little more of why I need the un-assign capabilities..
Thanks again
Eric
Ad the Lad
Sorry about that. You're correct -- this bug occurs after the cell parsing has been done. I don't know of any other workarounds short of manually updating the dataview with the DBNull.Value value. You can do this by creating a custom cell and overridding the SetValue method.
Again, please open a bug for this. Bugs found\entered by customers that don't have a workaround and which can cause dataloss are high priority for us right now
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"
tjeremenko
I'm also having the same problem with datagridviewcomboboxcolumns.
In an instance where a parent table entry can exist without a child table entry (ie the relationship is optional) the control will load the data correctly but ctrl+0 produces the reported error message.
Really, and in addition, I think delete and back space should be producing the same effect as ctrl+0.
WaveSlam
In my original post, I mentioned two instances where this is happening:
1) Let the user hit "Ctrl+0" to null out the assign to value
2) Add a blank item in the Assign To dropdown that has a blank for the displayMember and a null for the valueMemeber
Is your suggestion of handeling the keypress only a solution for Option 1 (Ctrl+0) Are you saying to ignore the Ctrl+0 key
Either way, how would I get the null in the employee_id column in the database using one of the scenarios listed above
Thank you for your reply.
Eric
CompactDev
I submitted a bug as requested two times :)
The bug number is: FDBK44783
Also, Is there a way to disable Ctrl+0
Thanks
Eric
Jiles
Thanks for taking the time to answer my post...
I added an event handler for keypress as requested. It apparently does not get fired when Ctrl+0 is pressed.
Even if I trap the Ctrl+0 keypress and set the combobox index to the DbNull item, I don't think it would work because of scenario two that I listed in the above post. When I choose the item in the combobox that has a DBNull ValueMember, I get the same error that occurs when I hit Ctrl+0.
Thanks
Eric
r_amarnath
This is a bug in the DataGridView. If you open a bug here: http://lab.msdn.microsoft.com/productfeedback/Default.aspx we might be able to fix it. I don't know of any workaround at this time apart from handling the keypress yourself
-mark
DataGridViewProgram Manager
Microsoft
This post is provided "as-is"
Merianos Nikos
No way to "disable", but you can override the ProcessDataGridViewKey and the ProcessDialogKey and check for the Keys.D0 or Keys.NumPad0.
-mark
DataGridViewProgram Manager
Microsoft
This post is provided "as-is"
KevinInIndy
Assuming you are databound, you'll want to store a DBNull.Value for the valueMember field and then handle the Ctrl+0 keypress and set the combobox's selectedindex to the entry with the DBNull.Value.
-mark
DataGridViewProgram Manager
Microsoft
This post is provided "as-is"
Joshua Wise
The problem is not with the DataGridView. The issue is that Tasks has an employee_id field that cannot accept null values. This is correct; you wouldnt want a task in your Tasks table that was not assigned to anyone. You're best be is probably to set the AllowNull value of the column of the DataTable to true. Then, when you are saving your results, for any rows that have that column as null, instead of saving that record to the database, delete it.
A "more correct" option would probably be to provide a delete button that deletes that row, instead of allowing them to set the employee dropdown to an empty value. Then you wouldnt have this issue.