Groups | Blog | Home
all groups > dotnet windows forms databinding > april 2005 >

dotnet windows forms databinding : .NET - ComboBox DataBinding


Bill S.
4/13/2005 2:53:33 PM
Hi,

To start, I need to explain the way information is setup within my tables. I
have three tables linked as parent/child relations. The first, or parent, is
my Properties table, and the Property's table child is the Units table, and
the Unit's table child is the Requests table. Now, within the Requests table
the fields are numbers, which are used as indexes into auxiliary tables
where the actual "long" string values reside. This system works fine when
displaying the information in my DataGrids. However, I need to open a dialog
using ComboBox controls. When I use the BindingContext for the Combos all I
see are the numbers, or indexes, (both in the "veiwing area" and in the drop
down list of the ComboBox). OK, so I set the ComboBox's DataSource,
DisplayMember, and ValueMember properties (in addition to using the
BindingContext) and it works ONLY while the ComboBox has focus. Once the
Combo loses focus it displays "System.Data.DataRowView" both within the
Combo's view and drop down list. I would appreciate if someone could advice
on how to solve this dilemma.

Thanks.



Beth Massi [Architect MVP]
4/13/2005 2:56:28 PM
Hi Bill,

Would it be possible to post your code? You also may want to read my blog
entry on Databinding and Comboboxes.

http://bethmassi.blogspot.com/2005/04/combobox-databinding-woes.html

-B

[quoted text, click to view]

Bill S.
4/13/2005 7:15:08 PM
Below is the code I use to bind several ComboBoxes. Also I use a simple
function to "clean" things up a bit (The tbl type is an enumeration).
Initially, all the values in all the ComboBoxes are correct. I can go to
each ComboBox and select a value. But, like I mentioned earlier it displays
some system error text or whatever.

Private Sub EdtDataSource(ByRef Edt As ComboBox, ByVal Table As tbl, ByVal
dm As String, ByVal vm As String)
Try
With Edt
.DataSource = DP.mDs.Tables(Table).DefaultView
.DisplayMember = dm
.ValueMember = vm
End With

Catch E As Exception
IMI.MsgErr("EdtDataSource: " & E.Message)
End Try
End Sub


Me.EdtWorkOrderNum.DataBindings.Add("Text", _
CType(DP.bmRequests.Current, DataRowView), "Work Order#")

Me.EdtServiceType.DataBindings.Add("DisplayMember", _
CType(DP.bmRequests.Current, DataRowView), "Service Type")
EdtDataSource(Me.EdtServiceType, tbl.ServiceTypes, "Service Type", "DataID")

Me.EdtDate.DataBindings.Add("Text", _
CType(DP.bmRequests.Current, DataRowView), "Date")

Me.EdtTime.DataBindings.Add("Text", _
CType(DP.bmRequests.Current, DataRowView), "Time")

Me.EdtWorkRequested.DataBindings.Add("Text", _
CType(DP.bmRequests.Current, DataRowView), "Work Requested")

Me.EdtInvoiceAmount.DataBindings.Add("Text", _
CType(DP.bmRequests.Current, DataRowView), "Invoice Amount")

Me.EdtServiceSchedule.DataBindings.Add("DisplayMember", _
CType(DP.bmRequests.Current, DataRowView), "Service Schedule")
EdtDataSource(Me.EdtServiceSchedule, tbl.ServiceSchedules, "Service
Schedule", "DataID")

Me.EdtContractor.DataBindings.Add("DisplayMember", _
CType(DP.bmRequests.Current, DataRowView), "Contractor")
EdtDataSource(Me.EdtContractor, tbl.Contractors, "Contractor", "DataID")

Me.EdtContractorFax.DataBindings.Add("DisplayMember", _
CType(DP.bmRequests.Current, DataRowView), "Contractor Fax")
EdtDataSource(Me.EdtContractorFax, tbl.Contractors, "Contractor Fax",
"DataID")

Me.EdtPriorityLevel.DataBindings.Add("DisplayMember", _
CType(DP.bmRequests.Current, DataRowView), "Priority Level")
EdtDataSource(Me.EdtPriorityLevel, tbl.Priorities, "Priority Level",
"DataID")

Me.EdtEnteredBy.DataBindings.Add("DisplayMember", _
CType(DP.bmRequests.Current, DataRowView), "Employee")
EdtDataSource(Me.EdtEnteredBy, tbl.Employees, "Employee", "DataID")



[quoted text, click to view]

Beth Massi [Architect MVP]
4/14/2005 10:25:04 AM
I *think* the problem may be in your databinding to DisplayMember. It seems
to me that you would want to bind to either the SelectedValue or the Text
property of the Comboboxes.

HTH,
-B

[quoted text, click to view]

AddThis Social Bookmark Button