Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. ADO.NET newbie: Binding to comboBox & querying a dataset

ADO.NET newbie: Binding to comboBox & querying a dataset

Scheduled Pinned Locked Moved .NET (Core and Framework)
databasecsharpwcfwpfcom
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andrew Connell
    wrote on last edited by
    #1

    Been using C# for a while now, but just never had to interact with a database (actually a running joke in the office... only dealt with web services, XML, sockets, and file IO). Anyway, I'm having a bit of a problem finding a solution to my challenge (two of them). Quick Overview: My application uses Access as a database (desktop app). On demand, I pull data from the Access database into a Dataset, using it as a working copy of the database. At anytime the user can save/reject his changes. Challenge 1: I am populating a combobox with values from one of my datatables. The value being added to the control is the "name" (not unique) of the record, but I have a unique ID for each record (int). When the user selects an option in the combobox, I want to be able to get the ID of the record the user selected. Currently, as I add items to the combobox, I add the ID's to an array where the combobox.selectedindex = index of the array. Makes it tough when they add a value to the combobox (I have to add it to the datatable, then flush and update the array and combobox so everything is synch'd. There MUST be a better way :-D This is my work around... just wanted to get it working and then go back and do it the right way. Challenge 2: Once my data is in the dataset, I need to be able to query it. The "datatable.contains" method helps, but what do I do when I need to find a specific record? I was looking for something that would do the following: DataRows theRecords = datatable.filter("intRecID=1"); However, I can't find a ".filter" or compareable item. Any ideas on how to overcome these two challenges?:confused: Just about run all leads & ideas down... Andrew Connell IM on MSN andrew@aconnell.com

    C 1 Reply Last reply
    0
    • A Andrew Connell

      Been using C# for a while now, but just never had to interact with a database (actually a running joke in the office... only dealt with web services, XML, sockets, and file IO). Anyway, I'm having a bit of a problem finding a solution to my challenge (two of them). Quick Overview: My application uses Access as a database (desktop app). On demand, I pull data from the Access database into a Dataset, using it as a working copy of the database. At anytime the user can save/reject his changes. Challenge 1: I am populating a combobox with values from one of my datatables. The value being added to the control is the "name" (not unique) of the record, but I have a unique ID for each record (int). When the user selects an option in the combobox, I want to be able to get the ID of the record the user selected. Currently, as I add items to the combobox, I add the ID's to an array where the combobox.selectedindex = index of the array. Makes it tough when they add a value to the combobox (I have to add it to the datatable, then flush and update the array and combobox so everything is synch'd. There MUST be a better way :-D This is my work around... just wanted to get it working and then go back and do it the right way. Challenge 2: Once my data is in the dataset, I need to be able to query it. The "datatable.contains" method helps, but what do I do when I need to find a specific record? I was looking for something that would do the following: DataRows theRecords = datatable.filter("intRecID=1"); However, I can't find a ".filter" or compareable item. Any ideas on how to overcome these two challenges?:confused: Just about run all leads & ideas down... Andrew Connell IM on MSN andrew@aconnell.com

      C Offline
      C Offline
      Chris Rickard
      wrote on last edited by
      #2

      Challenge 1: This is pretty easy to do. Say you have a DataSet with a DataTable named "Table 1" in "Table 1" you have Columns "id" and "name". You want "name" to be what the users see and "id" to be returned. comboBox1.DataSource = DataSet1.Tables["Table 1"]; comboBox1.DisplayMember = "name"; comboBox1.ValueMember = "id"; Then to get the associated id from the user's selection (I'm assuming ID is an AutoIncrimenting Integer) id = (int)comboBox1.Value; Challenge 2: You're close, very close. DataTable does not impliment a filter but DataView Does. And you can easily get a DataView from a DataTable by using the DefaultView Property thus: DataSet1.Tables["Table 1"].DefaultView.RowFilter = "intRecID=1"; See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataDataViewClassRowFilterTopic.asp?frame=true for more info.

      A 1 Reply Last reply
      0
      • C Chris Rickard

        Challenge 1: This is pretty easy to do. Say you have a DataSet with a DataTable named "Table 1" in "Table 1" you have Columns "id" and "name". You want "name" to be what the users see and "id" to be returned. comboBox1.DataSource = DataSet1.Tables["Table 1"]; comboBox1.DisplayMember = "name"; comboBox1.ValueMember = "id"; Then to get the associated id from the user's selection (I'm assuming ID is an AutoIncrimenting Integer) id = (int)comboBox1.Value; Challenge 2: You're close, very close. DataTable does not impliment a filter but DataView Does. And you can easily get a DataView from a DataTable by using the DefaultView Property thus: DataSet1.Tables["Table 1"].DefaultView.RowFilter = "intRecID=1"; See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataDataViewClassRowFilterTopic.asp?frame=true for more info.

        A Offline
        A Offline
        Andrew Connell
        wrote on last edited by
        #3

        Fantastic! you hit the nail on the head. Only thing I had to change from your code above was the .Value property doesn't exist so I replaced it with .SelectedValue. Thanks Chris! -AC Andrew Connell IM on MSN andrew@aconnell.com

        C 1 Reply Last reply
        0
        • A Andrew Connell

          Fantastic! you hit the nail on the head. Only thing I had to change from your code above was the .Value property doesn't exist so I replaced it with .SelectedValue. Thanks Chris! -AC Andrew Connell IM on MSN andrew@aconnell.com

          C Offline
          C Offline
          Chris Rickard
          wrote on last edited by
          #4

          Oops, got me there. Even in the .NET framework they can't keep these properties too consistent ;)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups