How to show ComboBox's SelectedValue if ComboBox is binded to a DataSet
I have a ComboBox
that is binded to a DataSet
. I wanted to show the selected value whenever the ComboBox
has a change in selection. I have the following code:
private void devCb1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
dv = new DataView(
dt,
"Device_ID = " + devCb1.SelectedIndex,
"Data_ID ASC",
DataViewRowState.CurrentRows);
dataDg1.ItemsSource = dv;
devCb1.DisplayMemberPath = "Content";
MessageBox.Show(devCb1.SelectedValue.ToString());
开发者_JAVA百科}
But it only gives me
System.Data.DataRowView
I have already set the DisplayMemberPath
, but it still not showing me the selected item's content. What is wrong?
[EDITED] I also tries the following
devCb1.SelectedValuePath = "Content";
MessageBox.Show(devCb1.SelectedValue.ToString());
But it also still gives me
System.Data.DataRowView
Doesn't work either..
DataRowView
has no content from what i can see. Your SelectedValuePath
should point to Row
i think (or if the current path correctly points to the Content
of the ComboBoxItem
you just need to cast at that point), and you'd still need to cast the SelectedValue
being a row, to that class and from there you can get some of its content.
Set breakpoints, use the debugger, look at the data in your objects.
Try SelectedValuePath to access the data you want to view
Well it is possible to access the data within DataRowView at least now (2 years after the question was made), by simply casting the SelectedValue to System.Data.DataRowView and accessing property Row["colName"] like this:
((System.Data.DataRowView)ComboBoxName.SelectedValue).Row["colName"];
精彩评论