bindinglist not synching

R

Rick

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
 
R

RobinS

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.
 
R

Rick

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
 
R

RobinS

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.
---------------------------
Rick said:
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

RobinS said:
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.
 
R

Rick

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
 
R

RobinS

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.
--------------------------
Rick said:
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

RobinS said:
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.
 
R

Rick

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

RobinS said:
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.
--------------------------
Rick said:
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

RobinS said:
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.
 
R

RobinS

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.
-------------------------------------
Rick said:
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

RobinS said:
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.
--------------------------
Rick said:
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.

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.
 
R

Rick

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
 
R

RobinS

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.
----------------------------------
 
R

RobinS

Rick,
I'm e-mailing you a small project. Too much to post here.
Robin S.
-------------------------------
 

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