.NET Framework Developer Center >
.NET Development Forums
>
Windows Presentation Foundation (WPF)
>
Saving a combobox item to the database.
Saving a combobox item to the database.
- I have a combobox with hard coded combobox items. Each combobox item has a tag which represent the value that I would like to save to the database. How do I pull the tag from a combobox item?
Is there an easier way to bind a comboboxitem?
Certified Geek. Data dude.
Answers
ComboBoxItem cbi = (ComboBoxItem)ComboType.SelectedItem;
if (cbi != null){
int type = Convert.ToInt32(cbi.Tag.ToString());
item.InventoryTypeId = type;
}
The above code works fine.
Certified Geek. Data dude.- Marked As Answer byGarvander Thursday, November 05, 2009 3:11 PM
All Replies
- Hi,
Could you please tell your scenario bit more clear?
What are you trying to do here?Some code sample should help
Thank You
Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem - Hi Garvander,
I'm not sure whether I understand your problem, if the ComboBox is set up in this way:
<ComboBox name="comboBox1">
<ComboBoxItem Content="a1" Tag="bbb1"/>
<ComboBoxItem Content="a2" Tag="bbb2"/>
<ComboBoxItem Content="a3" Tag="bbb3"/>
<ComboBoxItem Content="a4" Tag="bbb4"/>
</ComboBox>
Then you can get the Tag value directly from the ComboBoxItem:
private void button1_Click(object sender, RoutedEventArgs e)
{
foreach (ComboBoxItem item in comboBox1.Items)
{
Console.WriteLine(item.Tag .ToString());
}
}
If anything is unclear, please feel free to let me know.
Best Regards,
Zhi-Xin Ye
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! - The user needs to pick a value from a combobox. The selection is text and its id needs to be saved in the database. I am open to any solution the works.
Certified Geek. Data dude.- Edited byGarvander Thursday, November 05, 2009 1:19 PM
- Hi,
Get the Combobox SelectedItem whenever you want to save to the database.
Kindly tell what is the issue you are facing?
Thank You
Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem - which item in the foreach loop is the selected item?
Certified Geek. Data dude. - THe selected item is text and I want to save the corresponding id. Same way a select box works in HTML.
Certified Geek. Data dude. ComboBoxItem cbi = (ComboBoxItem)ComboType.SelectedItem;
if (cbi != null){
int type = Convert.ToInt32(cbi.Tag.ToString());
item.InventoryTypeId = type;
}
The above code works fine.
Certified Geek. Data dude.- Marked As Answer byGarvander Thursday, November 05, 2009 3:11 PM


