BindingSource Navigation

Hi,

I've got a Winform with a number of controls that are directly bound to a bindingsource, I've also created a list box and a combo box that will be used for quick record selection. The problem is that both the list box and combo box, have a custom query source. What I would like to know is how to navigator to the selected record and how to change the other control to the same record.

eg. If the user selects a record from the List Box, I want all the bound controls to go to that record and also the combo box to go to that record as well.

I've tried to use the Bindingsouce Find method but I get a FormatException error.

Thanks.


Answer this question

BindingSource Navigation

  • ada

    hi,

    still i don't understand where you listbox take its value anyway you can take a look to this code

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

    'retrieve some records to your bindingsource by useing  dataview

    'to be able to bind a value to listbox

    Dim dv As DataView

    dv = New DataView(Me.Db1DataSet.MytableName, "ColumnID >= 90 and ColumnID <= 100", "ColumnID", DataViewRowState.CurrentRows)

    'bind the filtered records to the listbox

    Me.ListBox1.DataSource = dv

    Me.ListBox1.DisplayMember = "ColumnName"

    Me.ListBox1.ValueMember = "ColumnID"

    End Sub

    Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown

    'Get the position of the selected item in the binding source

    Dim Recordposition As Integer = Me.MyBindingSource.Find("ColumnID", Me.ListBox1.SelectedValue)

    'change the binding source position to the new position

    Me.MyBindingSource.Position = Recordposition

    End Sub

    hope it helps



  • BertBeck

    Further to this...

    In the event ListBox1_SelectedValueChanged, I use the Me.ListBox1.SelectedValue.ToString to get the field data, but the event fires a number times returning a System.Data.DataRowView object. That errors on the BindingSource.find method.

    What is the preferred way to navigate to a record from a non bound List and Combo box

  • Joe Drummer

    hi,

    customer id is integer how can you search for text in integer field

    Dim Recordposition As Integer = Me.CustBindingSource.Find("customernamecolumn", text)

    or

    Dim Recordposition As Integer = Me.MyBindingSource.Find("CustID", Me.ListBox1.SelectedValue)

    hope that helps



  • jaime.rq

    Thanks again.

    Yes, I used the code below and funny enough it worked.

    Dim Recordposition As Integer = Me.CustBindingSource.Find("CustID", Me.ListCustomers.SelectedValue)

    Me.CustBindingSource.Position = Recordposition 'change the binding source position to the new position

    As a side question, What do you use the List Box's SelectedIndexChanged and SelectedValueChanged events for then


  • yoder30

    Yes, that helps.

    There seems to be a rather large jigsaw set to fit together if you want do more than drag and drop.


  • Lori J

    hi,

    sorry i forgot to say that, you can write the text to your listbox , but you have to write it the same as the data in your database and you can search the binding source by text not the id but again the text must be the same as your database column, in this case you will not need any Dataview

    also don't use listbox_selected index changed it will give you some silly message box at loading

    Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown

    'Get the position of the selected item in the binding source

    Dim text As String = Me.ListBox1.Items(Me.ListBox1.SelectedIndex)

    Dim Recordposition As Integer = Me.MyBindingSource.Find("ColumnName", text)

    'change the binding source position to the new position

    Me.MyBindingSource.Position = Recordposition

    End Sub



  • Jo&amp;#227;o Lopes

    hi,

    i didn't understand your question well, this is just my guessing

    if you  have all your controls bound to the same binding source then you don't have to write anycode

    if the listbox is not bound then you dont have selectedvalue you can use selectedindex instead or selecteditem depends on which data you want

    Hope it will help



  • stan_v

    One more thing. If I have a combo box with the same data, how can I have it show the same value as the List Box I've got the Combo box sorted in the opposite direction and the code below selected the wrong value.

    Me.cboCustomers.SelectedValue() = Me.ListCustomers.SelectedValue

    It looks like its using a position index not the actual CustID value...

    I made the Sorted field in the combo box false instead of true and it works. Does this field sort the displayed data only Does this mean that I will have to create a custom query for each control


  • BOMBAY_108

    Ok. Thanks, that makes more sense now. I just tried it with my code and I get a conversion error "Conversion from type 'DataRowView' to type 'String' is not valid".

    I want to be able to use custom SQL queries if it is allowable.

    The code I'm using is as below:

    Dim scnn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\Projects\Test\Test.mdb"

    Dim sCustSql As String = "SELECT TOP 10 CustID, FName & ' ' & LName AS FullName FROM Cust ORDER BY FName DESC"

    Me.cboCustomers.DataSource = GetTable(sCustSql, scnn)

    Me.cboCustomers.DisplayMember = "FullName"

    Me.cboCustomers.ValueMember = "CustID"

     

    Friend Function GetTable(ByVal sSql As String, ByVal sCnn As String) As DataTable

    Dim myCnn As New OleDbConnection(sCnn)

    Dim myDa As New OleDbDataAdapter(sSql, myCnn)

    Dim dt As New DataTable()

    myDa.Fill(dt)

    Return dt

    End Function

     

    Private Sub ListCustomers_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListCustomers.MouseDown

    ''<< -- Errors on the line below -- >>

    Dim text As String = Me.ListCustomers.Items(Me.ListCustomers.SelectedIndex.ToString)

    Dim Recordposition As Integer = Me.CustBindingSource.Find("CustID", text)

    'change the binding source position to the new position

    Me.CustBindingSource.Position = Recordposition

    End Sub


  • InfoGenesis

    hi,

    selectedindexchanged event handler you can use it to capture the user events after your form started and your bindingsources started to work if you use it to build something during form starting in will give you the messegbox blah blah blah dataview that you was talking about unless all your datasources strongly typed and working all at the same time like the dataset and bindingsources

    when i tried selectedindexchanged event it did something like capture the event b4 it happend like for example it display customer 3 and i opened the combobox and select 5 , when i use it to change other bindingsource position it will pass 3(the old value), if i select 7 it will pass 5 and so on

    i think you was talking about binding source not to binddata to controls from the database directly, actualy this is the use for the binding sources it will unify the data in all the controsl, you dont' have to use many queries like that you can add it to your origional query "order by customername Desc"

    hope that helps

     



  • Chamendra Perera

    Ok. Let me put it another way. Say the dataset has 100 records, with an ID field starting from 1 and going to 100. Say then also I have an unbound Listbox that shows only the records with the ID's from 90 to 100. From what I've seen if I use the SelectedIndex, it returns a number from 0 to 9, so when I apply that to the BindingSource, it moves to the ID records 1 to 9.

    What I'm looking for is a standard way to do something as simple as in MS Access where you can have:


    Me.CustID.Setfocus
    DoCmd.FindRecord Me.ListCustomers

    What is this best way of navigation when you have a control that isn't bound

    Unfortunately, the reference material I have is for .Net 1.1 and only talks about Me.BindingContext(DataSet1.Customers).Position = value

    So I'm guessing that it's different now in 2005


  • BindingSource Navigation