user1704812 user1704812 - 4 months ago 8
Vb.net Question

How to bind a button into a particular element in dataset in VB.net?

I can't seem to figure out how to bind a given element to a button on a Windows form.

When I go to

DataBindings
under the control's properties, I get a list of the columns, and then I click on one column called
ID
.

I get
ItemsBindingSource - ID
.

What is the proper syntax to choose a given element from the selected column.

Uno Uno
Answer

You need to set the current item on the CurrencyManager:

Dim cm = CType(Me.BindingContext(<DataSource>), CurrencyManager)

' This will change the current record and trigger the update of the Button.
cm.Position += 1

If it's an autogenerated source from a SQL table, should be: Me.TableBindingSource.

To select a record based on some criteria, you can find the position of the item in the data source and set it to the manager:

Dim element = cm.List.OfType(Of DataRowView).Single(function(view) view.Item("Column").ToString() = "Value")

cm.Position = cm.List.IndexOf(element)

There are simple examples here:

http://www.akadia.com/services/dotnet_databinding.html

Comments