reading data from dataset bound to a combo box.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hetal

    reading data from dataset bound to a combo box.

    Hi there..

    I am a VB6 developer so kinda trying to figure how to work with
    VB.NET. I have a scenario where i have a DataSet bound to a combo box,
    and i would like to read a row from the DataSet based on the item
    selected in the Combo Box.

    For e.g.
    I have a dataset that has 1 table containing 3 fields say ID, Name,
    and Address.
    This dataset is bound to a Combo Box with ID as a ValueMember and Name
    as a DisplayMember.
    The Combo Box fills up perfectly with the Name field.

    The problem i am facing is, when i select a Name from the Combo Box, i
    would like to read the Address for that Name from the DataSet.

    I did find a few samples but they would go through the loop to read
    rows in a DataSet, but i could not find a sample where i can read a
    row from a DataSet for a selected value.

    Any help on this one will be much appreciated. Thanks.

    Hetal.

  • sloan

    #2
    Re: reading data from dataset bound to a combo box.


    If youre workign with winforms (as opposed to asp.net) the number 1 thing to
    remember/know about comboboxes.

    You put (some kind of) object in a combobox, and NOT value/text pairs.

    What do I mean?

    If you have a class

    public class Emp

    and a collection of Emp's
    public class EmpCollection

    and you bind a combobox to some EmpCollection

    Each item in the combobox is an Emp object.

    You probably have a DataRow object in each item of the combobox.

    You'll have to pull it out, and cast it to work with it.




    "Hetal" <hetal.a.kapadi a@gmail.comwrot e in message
    news:1177516570 .123550.206560@ c18g2000prb.goo glegroups.com.. .
    Hi there..
    >
    I am a VB6 developer so kinda trying to figure how to work with
    VB.NET. I have a scenario where i have a DataSet bound to a combo box,
    and i would like to read a row from the DataSet based on the item
    selected in the Combo Box.
    >
    For e.g.
    I have a dataset that has 1 table containing 3 fields say ID, Name,
    and Address.
    This dataset is bound to a Combo Box with ID as a ValueMember and Name
    as a DisplayMember.
    The Combo Box fills up perfectly with the Name field.
    >
    The problem i am facing is, when i select a Name from the Combo Box, i
    would like to read the Address for that Name from the DataSet.
    >
    I did find a few samples but they would go through the loop to read
    rows in a DataSet, but i could not find a sample where i can read a
    row from a DataSet for a selected value.
    >
    Any help on this one will be much appreciated. Thanks.
    >
    Hetal.
    >

    Comment

    • Hetal

      #3
      Re: reading data from dataset bound to a combo box.

      Thank you sloan. I created an object of class DataRowView and i am
      casting the item selected in Combo Box to DataRowView. Keeping the
      previous example in mind, here is the snippet of code that i wrote to
      get the Address from DataSet.

      'In Declaration section.
      Dim drvStores As DataRowView

      'Code to read the data from dataset based on combo item selection.
      Private Sub cmbStores_Selec tedIndexChanged (ByVal sender As
      System.Object, ByVal e As System.EventArg s) Handles
      cmbStores.Selec tedIndexChanged
      drvStores = DirectCast(cmbS tores.SelectedI tem, DataRowView)
      MsgBox(drvStore s("Address"))
      End Sub

      Am i heading the right direction? I mean, is this an efficient way of
      reading data from a dataset? Thanks.

      Comment

      • Hetal

        #4
        Re: reading data from dataset bound to a combo box.

        oh.. i missed mentioning that i am working with windows forms. thanks.

        Comment

        • sloan

          #5
          Re: reading data from dataset bound to a combo box.


          Youre on the right path.

          Private Sub cmbStores_Selec tedIndexChanged (ByVal sender As
          System.Object, ByVal e As System.EventArg s) Handles
          cmbStores.Selec tedIndexChanged

          'try this

          dim o as object
          o = cmbStores.Selec tedItem
          if not (o is nothing) then
          Console.Write ( o.GetType().ToS tring() )
          'put a watch on "o" in the debugging window, and it should tell you
          the type also
          end if



          ' drvStores = DirectCast(cmbS tores.SelectedI tem, DataRowView)
          ' MsgBox(drvStore s("Address"))



          End Sub

          the dim o as object is ONLY FOR FIGURING OUT WHAT YOU GOT.
          DO NOT LEAVE THAT IN THERE.

          after you have the object type, then you can cast.


          PS

          You might want to bind your combobox to the dataset.TABLE , not just hte
          dataset.

          Ex:

          if not ( ds.Tables(0) is nothing) then

          cbo1.DataSource = ds.Tables(0)

          end if

          OR better (if its a strong typed dataset)


          if not ( ds.Employee is nothing) then

          cbo1.DataSource = ds.Employee

          end if


          where Employee is a strong table name in your dataset.




          "Hetal" <hetal.a.kapadi a@gmail.comwrot e in message
          news:1177601581 .590144.129580@ t38g2000prd.goo glegroups.com.. .
          Thank you sloan. I created an object of class DataRowView and i am
          casting the item selected in Combo Box to DataRowView. Keeping the
          previous example in mind, here is the snippet of code that i wrote to
          get the Address from DataSet.
          >
          'In Declaration section.
          Dim drvStores As DataRowView
          >
          'Code to read the data from dataset based on combo item selection.
          Private Sub cmbStores_Selec tedIndexChanged (ByVal sender As
          System.Object, ByVal e As System.EventArg s) Handles
          cmbStores.Selec tedIndexChanged
          drvStores = DirectCast(cmbS tores.SelectedI tem, DataRowView)
          MsgBox(drvStore s("Address"))
          End Sub
          >
          Am i heading the right direction? I mean, is this an efficient way of
          reading data from a dataset? Thanks.
          >

          Comment

          • Hetal

            #6
            Re: reading data from dataset bound to a combo box.

            Thanks Sloan. It was a very helpful piece of info.

            Comment

            Working...