Databinding ComboBox with NULL values

G

Guest

VS.NET 2003

I have a Windows Form application, with a form that has various controls,
all of which are bound to a custom business object:

Class Employee

Private _employeename As String
Public Property EmployeeName() As String
Get
Return _employeename
End Get
Set(ByVal Value As String)
_employeename = Value
End Set
End Property

Private _positionid As String
Public Property PositionID() As String
Get
Return _positionid
End Get
Set(ByVal Value As String)
_positionid = Value
End Set
End Property

End Class

SIDENOTE: Yup, I'm stuck with the PositionID being a string instead of the
more sensible integer because of the existing database schema.

My question revolves around the fact that the PositionID can be NULL, and
the user needs to be able to set it to NULL as well. On my form, the
PostionID is selectable by the user with a ComboBox. Here is how the
ComboBox's DataSource is setup:

Dim positions As DataTable = BuildDataTable()
ComboBox1.DisplayMember = "PositionType"
ComboBox1.ValueMember = "PositionID"
ComboBox1.DataSource = positions

The ComboBox is bound to the Employee object as follows (like the other
controls on the form):

ComboBox1.DataBindings.Add("SelectedValue", emp, "PositionID")

The ComboBox's DropDownStyle is set to DropDown. I would like for the user
to be able to simply delete the ComboBox's text and have an empty string ("")
be propagated back Employee business object.

The only way I have found to do this is the following 2-part process:

1) add an extra row with "" as its value to the DataTable that is the
ComboBox's DataSource:

dt.Rows.Add(New Object() {"", ""})

2) add the following code to the ComboBox's KeyUp event handler:

If (ComboBox1.Text = "") Then
ComboBox1.SelectedIndex = ComboBox1.FindStringExact("")
End If

I would like to eliminate the part where i have to add a blank row to the
DataTable, is there a way to do this?

Please let me know if I need to provide more information.

Thanks!
David McClelland
 

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