ADO.NET newbie: Binding to comboBox & querying a dataset
-
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 -
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.comChallenge 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.
-
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.
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 -
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.comOops, got me there. Even in the .NET framework they can't keep these properties too consistent ;)