.NET Forum / Windows Forms / WinForm Data Binding / April 2007
bindinglist not synching
|
|
Thread rating:  |
Rick - 31 Mar 2007 12:59 GMT VS 2005
I have a class of BindingList(of T) to use as a datasource for a lookup combobox. The combobox is to lookup the Terms for a Purchase Order.
When I implement this on my Winform, the combobox does not synch with the underlying data, i.e. the Terms description does not show for the terms number.
The combobox does show the Descriptions in the drop-down box, so they are being filled into the binding list, they just don't synch with the underlying data.
Can anyone see what is wrong?
Rick
**** combox box is connected like this *** cbTerms.DisplayMember = "Name"
cbTerms.ValueMember = "Num"
Dim bs As LookupNVP = New LookupNVP(LookupNVP.listType.Terms, fMain.conn)
cbTerms.DataSource = bs
cbTerms.DataBindings.Clear()
cbTerms.DataBindings.Add("SelectedValue", PObind, "Termsnum")
**** class for bindinglist ****
Public Class LookupNVP
Inherits BindingList(Of nvp)
Public Enum listType
Terms
Shipvia
End Enum
Public Class nvp
Private _name As String
Private _num As Integer
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property
Public ReadOnly Property Num() As Integer
Get
Return _num
End Get
End Property
Private Sub New()
End Sub
Public Sub New(ByVal newValue As Integer, ByVal newName As String)
_name = newName
_num = newValue
End Sub
End Class
Public Sub New(ByVal list As listType, ByVal conn As FbConnection)
MyBase.New()
Dim sql As String
Select Case list
Case listType.Shipvia
sql = "Select SHIPVIANUM, METHOD || ' [' || SHIPVIANUM || ']' " _
& "from SHIPVIA order by METHOD"
Case listType.Terms
sql = "Select TERMSNUM, DESCRIPTION || ' [' || TERMSNUM || ']' " _
& "from TERMS order by DESCRIPTION"
End Select
If sql Is Nothing Then Return
Dim cmd As FbCommand = New FbCommand(sql, conn)
'Dim reader As FbDataReader
Try
conn.Open()
Using reader As FbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While reader.Read
Me.Add(New nvp(reader.GetInt32(0), reader.GetString(1)))
End While
reader.Close()
End Using
Finally
conn.Close()
End Try
End Sub
End Class
RobinS - 01 Apr 2007 06:24 GMT Override the ToString method in your LookupNVP class.
By the way, when you post your code, if you paste it into Notepad, then copy and paste it into your posting, it will retain its spacing, and be easier to read. The easier it is to read, the more likely someone will help you.
Robin S. ---------------------------
> VS 2005 > [quoted text clipped - 134 lines] > > End Class Rick - 01 Apr 2007 15:03 GMT Thanks for the notebook tip.
I overrode the ToString method, but still no joy. The addition is ******** below.
What did I do wrong?
Public Class LookupNVP Inherits BindingList(Of nvp)
Public Enum listType Terms Shipvia End Enum
Public Class nvp Private _name As String Private _num As Integer
Public ReadOnly Property Name() As String Get Return _name End Get End Property Public ReadOnly Property Num() As Integer Get Return _num End Get End Property
Private Sub New() End Sub
Public Sub New(ByVal newValue As Integer, ByVal newName As String) _name = newName _num = newValue End Sub
'******************************************** Public Overrides Function ToString() As String Return Me.Name End Function
'*********************************************
End Class
Public Sub New(ByVal list As listType, ByVal conn As FbConnection) MyBase.New() Dim sql As String Select Case list Case listType.Shipvia sql = "Select SHIPVIANUM, METHOD || ' [' || SHIPVIANUM || ']' " _ & "from SHIPVIA order by METHOD" Case listType.Terms sql = "Select TERMSNUM, DESCRIPTION || ' [' || TERMSNUM || ']' " _ & "from TERMS order by DESCRIPTION" End Select
If sql Is Nothing Then Return
Dim cmd As FbCommand = New FbCommand(sql, conn) 'Dim reader As FbDataReader Try conn.Open() Using reader As FbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) While reader.Read Me.Add(New nvp(reader.GetInt32(0), reader.GetString(1))) End While reader.Close() End Using
Finally conn.Close() End Try
End Sub
End Class
> Override the ToString method in your LookupNVP class. > [quoted text clipped - 4 lines] > > Robin S. RobinS - 02 Apr 2007 07:55 GMT Move your nvp class out of your lookupNvp class.
Public Class LookupNVP Inherits BindingList(of nvp) ... End Class
Public Class NVP ... (ToString goes here) End Class
Put the reading of the database in LookupNVP, and add each NVP object to your list.
Robin S. ---------------------------
> Thanks for the notebook tip. > [quoted text clipped - 88 lines] >> >> Robin S. Rick - 02 Apr 2007 10:19 GMT Thanks Robin,
Still no joy. All the items appear in the combobox, they just don't lookup when the data changes.
Can you think of something else?
Public Class nvp Private _name As String Private _num As Integer
Public Property Name() As String Get Return _name End Get Set(ByVal value As String) _name = value End Set End Property Public Property Num() As Integer Get Return _num End Get Set(ByVal value As Integer) _num = value End Set End Property
Public Sub New() End Sub
Public Sub New(ByVal newValue As Integer, ByVal newName As String) _name = newName _num = newValue End Sub
Public Overrides Function ToString() As String Return _name End Function
End Class
Public Class LookupNVP Inherits BindingList(Of nvp)
Public Enum listType Terms Shipvia End Enum
Public Sub New(ByVal list As listType, ByVal conn As FbConnection) MyBase.New() Dim sql As String Select Case list Case listType.Shipvia sql = "Select SHIPVIANUM, METHOD || ' [' || SHIPVIANUM || ']' " _ & "from SHIPVIA order by METHOD" Case listType.Terms sql = "Select TERMSNUM, DESCRIPTION || ' [' || TERMSNUM || ']' " _ & "from TERMS order by DESCRIPTION" End Select
If sql Is Nothing Then Return
Dim cmd As FbCommand = New FbCommand(sql, conn)
conn.Open() Using reader As FbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection) While reader.Read() Me.Add(New nvp(CInt(reader(0).ToString), reader(1).ToString)) End While reader.Close() End Using
End Sub
End Class
> Move your nvp class out of your lookupNvp class. > [quoted text clipped - 12 lines] > > Robin S. RobinS - 03 Apr 2007 06:24 GMT I'm not completely understanding. You're binding the data for the combobox to the list(of nvp), right? So those items are showing just fine?
So what do you mean by "they don't lookup when data changes" ? Do you need the combobx to be double-bound so in addition to showing a list of possibilities, it displays the actual one selected?
To do this, set the SelectedValue property to the field in the other list or whatever it is you are binding the rest of your controls to.
Does that help?
Robin S. --------------------------
> Thanks Robin, > [quoted text clipped - 94 lines] >> >> Robin S. Rick - 03 Apr 2007 11:45 GMT Yes, you got it.
In this specific case I am loading all the possible terms into the cb using their descriptions for the text in the combo box. The terms number is the cb value member.
The selected value is the purchase order terms number.
All of the terms descriptions show in the cb after I bind DisplayMember and ValueMember, but when I move to a purchase order the cb shows a blank selection rather than the corresponding terms description (displaymember).
I am binding the cb like this:
cbTerms.DisplayMember = "Name"
cbTerms.ValueMember = "Num"
cbTerms.DataSource = New LookupNVP(LookupNVP.listType.Terms, fMain.conn)
cbTerms.DataBindings.Clear()
cbTerms.DataBindings.Add("SelectedValue", PObind, "Termsnum")
So what I assume is happening is that a change in the purchase order termsnum causes a lookup in the LookupNVP binding list(of NVP) and since it does not locate the value it returns -1.
BTW when I create a datatable/tableadapter/bindingsource for the terms and bind this to the cb, everything works as expected. I just don't need all this extra overhead for the fixed terms choices so I wanted to cut it to the minimum with a fixed list bound to the cb.
Thanks for your persistance.
Rick
> I'm not completely understanding. You're binding the data for the combobox > to the list(of nvp), right? So those items are showing just fine? [quoted text clipped - 108 lines] >>> >>> Robin S. RobinS - 04 Apr 2007 05:59 GMT Why are you doing this? You're clearing the data binding on your combobox after you've set it.
> cbTerms.DataBindings.Clear() Try this:
cbTerms.DataBindings.Add(New Binding("SelectedValue", PObind, "Termsnum", True)) cbTerms.DataSource = New LookupNVP(LookupNVP.listType.Terms, fMain.conn) cbTerms.DisplayMember = "Name" cbTerms.ValueMember = "Num"
Robin S. -------------------------------------
> Yes, you got it. > [quoted text clipped - 147 lines] >>>> >>>> Robin S. Rick - 04 Apr 2007 12:28 GMT I was doing the cbTerms.DataBindings.Clear() because the databindings were already set in the designer for the datatable connection that I am using in the meantime.
No, that doesn't work either. The "lookup" does not happen so when I change rows in the PurchaseOrder table, the terms combo box has an index of -1.
Do you by chance have any example code for a class anything like this that does work as a source for a lookup combo box? That might be easier for me to adapt.
Thanks,
Rick
> Why are you doing this? You're clearing the data binding on your combobox > after you've set it. [quoted text clipped - 10 lines] > > Robin S. RobinS - 05 Apr 2007 06:34 GMT Yes, I do, but I don't have time to post it tonight, and I'm going to be in an all-day design meeting tomorrow. :-( I'll try to get to it on Friday, unless I do it tomorrow night (50/50 chance).
Robin S. ----------------------------------
>I was doing the cbTerms.DataBindings.Clear() because the databindings were >already set in the designer for the datatable connection that I am using [quoted text clipped - 26 lines] >> >> Robin S. RobinS - 09 Apr 2007 02:43 GMT Rick, I'm e-mailing you a small project. Too much to post here. Robin S. -------------------------------
> Yes, I do, but I don't have time to post it tonight, and I'm going to be > in an all-day design meeting tomorrow. :-( I'll try to get to it on [quoted text clipped - 33 lines] >>> >>> Robin S.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|