I have the following code to add items to a combobox and add a tag to the Id for the items add
Dim str As String
Me.CostItemsTableAdapter.FillByProjectId(Me.BuildersEstimatorDatabaseDataSet.CostItems, StartPage.CurrentProjectId)
Me.ToolStripComboBox1.Items.Clear()
Dim row As BuildersEstimatorDatabaseDataSet.CostItemsRow
For Each row In BuildersEstimatorDatabaseDataSet.CostItems.Rows
If row.CostCode.Length > 0 Then
str = row.CostCode & " - "
Else
str = ""
End If
ToolStripComboBox1.Items.Add(str & row.CostName)
Me.ToolStripComboBox1.Tag = row.CostId
Next
If Me.ToolStripComboBox1.Items.Count > 0 Then
Me.ToolStripComboBox1.SelectedIndex = 0
Else
Me.ToolStripComboBox1.SelectedIndex = -1
End If
I am having a problem getting the tag value when that item is selected in the combobox. How is the best way to retrieve the tag id for the items selected?
Thanks |
| Randy0128 Friday, October 24, 2008 1:06 PM |
Hi Randy0128
I think your case can be well solved if you use a DataBinding. You can bind your ComboBox to the DataTable (CostItems in your case), then set its DisplayMember and ValueMember like this.
Code Snippet
Me.BuildersEstimatorDatabaseDataSet.CostItems
"CostCode"
"CostId"
So you can get the value of currently selected item by this.
Code Snippet
If you have any question in doing this, please don’t hesitate to tell me.
Sincerely,
Kira Qian |
| Kira Qian Monday, October 27, 2008 4:31 AM |
If you have used the "CostId" as the ValueMember (as Kira suggested), then your SelectedValue should be whatever is the value in the CostID column of your DataSet. Also, in order to display both the CostCode and the CostName in your combo, you need to concatenate the two into another column (ex: Description), and then use that column as the DisplayMember.
|
| BonnieB Monday, October 27, 2008 8:54 PM |
Hi Randy0128
I think your case can be well solved if you use a DataBinding. You can bind your ComboBox to the DataTable (CostItems in your case), then set its DisplayMember and ValueMember like this.
Code Snippet
Me.BuildersEstimatorDatabaseDataSet.CostItems
"CostCode"
"CostId"
So you can get the value of currently selected item by this.
Code Snippet
If you have any question in doing this, please don’t hesitate to tell me.
Sincerely,
Kira Qian |
| Kira Qian Monday, October 27, 2008 4:31 AM |
Thanks for your help. I am trying to get the 'CostCode' and the 'CostName' into the combobox. I am also having a hard time getting the costId from 'combobox1.SelectedValue'. I have the tableadapter set to fill by costId. Doed the code 'comboBox1.selectedValue' give the integer value that is in the CostId column in the datatable.
Thanks
|
| Randy0128 Monday, October 27, 2008 4:15 PM |
If you have used the "CostId" as the ValueMember (as Kira suggested), then your SelectedValue should be whatever is the value in the CostID column of your DataSet. Also, in order to display both the CostCode and the CostName in your combo, you need to concatenate the two into another column (ex: Description), and then use that column as the DisplayMember.
|
| BonnieB Monday, October 27, 2008 8:54 PM |
Thanks I will work on concatenate the to items into one column. I was able to solve the problem for the selectedvalue by moving the combobox1.data code after the displaymember and valuemember code.
Thanks
|
| Randy0128 Monday, October 27, 2008 9:07 PM |