NULLABLE datagridviewcomboboxcolumn (vbnet 2005)


A

alex

I have a datagridview with datagridviewcomboboxcolumn bind to a nullable
integer datatable field
When I add a new row I can do it without select any item on combo and the
NULL value in inserted on the datatable
But if I select an Item than there is no way to unselect it and choose null
Even I if use ctrl+0 null text is passed to the combo, but then I get a
commit error
I've tryed with this but is not really a nice solution
Private Sub DataGridView_dettaglio_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles DataGridView_dettaglio.KeyUp

If e.KeyCode = Keys.Delete And
Me.DataGridView_dettaglio.CurrentCell.ReadOnly = False Then

' delete the entry by giving the cell the value dbnull

Me.DataGridView_dettaglio.CurrentCell.Value = DBNull.Value

End If

End Sub

How can I do it?
Thank you
Alessandro
 
L

Linda Liu[MSFT]

Hi Alessandro,

I suggest that you add a special row in the lookup table that is bound to
the DataSource property of the DataGridViewComboBoxColumn. The special row
has a dbnull value for the field which is set as the value member.

For example, the lookup table has two columns named "ID" and "Name"
respectively. The following code adds some rows including the special row
into the lookup table.

'define a DataTable and add two columns into it
Dim lookup As New DataTable
Dim col As New DataColumn("ID", GetType(Integer))
lookup.Columns.Add(col)
col = New DataColumn("Name")
lookup.Columns.Add(col)

'add some rows into the DataTable
Dim row As DataRow = lookup.NewRow
'this is the special row added into the lookup table
'I don't set the value for the first column in the row because the
default value of a DataColumn is DBNull
row(1) = " "
lookup.Rows.Add(row)

row = lookup.NewRow
row(0) = 1
row(1) = "a"
lookup.Rows.Add(row)

row(0) = 2
row(1) = "b"
lookup.Rows.Add(row)

Note that the value of the Name field of the special row mustn't equal to
the NullValue property of the DataGridView cell. The default value of the
NullValue property of a DataGridView cell is an empty string "". Otherwise,
when the user selects the special item, the DataGridView will set the value
of the current DataGridViewCell to null, but the underlying data source
won't accept null as a valid value, so an error occurs at this time.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ad

Advertisements

G

Guest

I've tryed but I have the same problem.
When I am inserting a new row I can select all value, included the null that
is displayed as blank.
If I save the newrow with a null value it work fine, but then If I edit the
row, select an Item, save the row and select again the null item I get a
COMMIT error from the grid and the previuos value is selected on the combo.
 
Ad

Advertisements

L

Linda Liu[MSFT]

Hi Alessandro,

Thank you for your prompt reply!

I have created a sample project and it works fine on my side. I will send
it to your email box.

After you receive my email, save the attachment on your machine. Build the
sample project and press F5 to run the application.

Add/modify rows in the DataGridView and then click the 'save' button on the
form to apply the pending changes to the underlying data source.

Click the 'print values' button to print values of the rows in the
DataGridView to the Output window. You can check if the values in the
DataGridView are correct or not.

If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top