Link to home
Start Free TrialLog in
Avatar of Declan Basile
Declan BasileFlag for United States of America

asked on

Saving a Null Value to a DataTable when a bound combobox is Cleared

In vb.net, I have a combobox's "SelectedValue" property bound to a field of a datatable.  When I select an item from the combobox, the SelectedValue gets saved and the ColumnChanged event of the DataTable is triggered; however, when I clear the combobox, neither the SelectValue gets saved to the DataTable nor does the ColumnChanged event get triggered.  How can I get a Null value to be saved to the bound field of the DataTable when the combobox is cleared?
Avatar of Rikin Shah
Rikin Shah
Flag of India image

Hi,

Have you tried to do using following statement-

comboBox1.SelectedIndex = -1
Avatar of Declan Basile

ASKER

The Selected Index becomes -1 when when the selection is cleared.  My problem is that it doesn't trigger an update of Null back to the bound field.
I ended up writing the following event handler to handle the validating event  ...

        Dim cbx As acComboBox

        cbx = CType(sender, acComboBox)

        If cbx.Text = String.Empty Then
            If cbx.DataBindings.Count > 0 Then
                cbx.DataBindings(0).WriteValue()
            End If
            Exit Sub
        End If

        If cbx.SelectedItem Is Nothing Then
            MessageBox.Show("Enter an Item from the List.")
            e.Cancel = True
        End If

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Declan Basile
Declan Basile
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial