BlueOysterCult Posted March 18, 2004 Posted March 18, 2004 Hello I need to fill combo boxes on my form at form_load - I am not sure how to do it - I know it involves a For each loop - datase table something- combobox.add.item(dataset(dataRow) I have six tables only need to use five to fill the combos. they are make(car) interiorColor exteriorColor year soldstatus. these are all derived(Children?) of the Vehicles dataSet... excuse me for my lack of knowing which these are. Any help would be appreciated R Quote
Denaes Posted March 18, 2004 Posted March 18, 2004 The For Each method: Dim ds As DataSet Dim dr As DataRow For Each dr In ds.Tables("TableName").Rows 'This For Each loop will run through the tables 'collection with dr acting as each datarow (record) 'in the table specified. 'Choose which item you're goin to add to which combobox ComboBox1.Items.Add(dr.Item("ColumnName").ToString) 'This loop will loop through every datarow in your 'datatable, allowing you to add each value of a particular 'column to your combobox. Next Datasourced: ComboBox1.DataSource = ds.Tables("TableName") ComboBox1.DisplayMember = ds.Tables("TableName").Columns("ColumnName").ToString The second is much cleaner and faster and has some advantages when dealing with things like a listbox which can change records depending on which item you select. The first has the advantage of you being able to play with your data. You can choose to concatonate two datacolumns (LastName, FirstName; Car, CarYear) and to sort them via the combo/listbox's sorted property (you can do the same in a datasource using the DataView.sort method). You can also add further items and change old ones at a later date. But then you have to do searches to find out which record text is from, and that's also more complex. Quote
BlueOysterCult Posted March 18, 2004 Author Posted March 18, 2004 That's fantastic!! Thank You! Do I need some sort of counter? maxCount +=1 ???? Rob The For Each method: Dim ds As DataSet Dim dr As DataRow For Each dr In ds.Tables("TableName").Rows 'This For Each loop will run through the tables 'collection with dr acting as each datarow (record) 'in the table specified. 'Choose which item you're goin to add to which combobox ComboBox1.Items.Add(dr.Item("ColumnName").ToString) 'This loop will loop through every datarow in your 'datatable, allowing you to add each value of a particular 'column to your combobox. Next Datasourced: ComboBox1.DataSource = ds.Tables("TableName") ComboBox1.DisplayMember = ds.Tables("TableName").Columns("ColumnName").ToString The second is much cleaner and faster and has some advantages when dealing with things like a listbox which can change records depending on which item you select. The first has the advantage of you being able to play with your data. You can choose to concatonate two datacolumns (LastName, FirstName; Car, CarYear) and to sort them via the combo/listbox's sorted property (you can do the same in a datasource using the DataView.sort method). You can also add further items and change old ones at a later date. But then you have to do searches to find out which record text is from, and that's also more complex. Quote
Denaes Posted March 18, 2004 Posted March 18, 2004 That's fantastic!! Thank You! Do I need some sort of counter? maxCount +=1 ???? Rob None needed, unless you're keeping track for some reason Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.