Reputation: 33
I want to get selected item in combobox wpf. But it returns System.Data.DataRowView. My code in xaml :
<ComboBox Name="ddDeputi" Margin="131,85,0,0" Width="327" HorizontalAlignment="Left" VerticalAlignment="Top" Height="22"
SelectedValue="{Binding Path = kodeDep, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
Validation.ErrorTemplate="{x:Null}" SelectionChanged="ddDeputi_SelectionChanged" Loaded="ddDeputi_Loaded" />
And in my .cs :
private void ddDeputi_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ddDeputi.SelectedIndex != -1)
{
string akode = ddDeputi.SelectedValue.ToString();
DaUnitKerja oDa = new DaUnitKerja();
DataSet data = new DataSet();
data = oDa.TampilDir(akode);
ddDir.ItemsSource = data.Tables[0].DefaultView;
ddDir.DisplayMemberPath = data.Tables[0].Columns["unit_kerja"].ToString();
ddDir.SelectedValuePath = data.Tables[0].Columns["kode"].ToString();
ddSubDir.SelectedIndex = -1;
ComboBoxItem cbi = (ComboBoxItem)ddDeputi.ItemContainerGenerator.ContainerFromItem(ddDeputi.SelectedItem);
txtDeputi.Text = cbi.Content.ToString();
}
}
Upvotes: 1
Views: 5225
Reputation: 1205
When you have defined selection changed event u can easily get any value from the table you bind to ddDeputi by the index value .... Use comboBox1.SelectedIndex for the table index in event ddDeputi_SelectionChanged..
Like this
String value = data.Tables[0].Rows[ddDeputi.SelectedIndex]["kode"].ToString();
and the other way is cast your combo box selected item in comboboxitem and use it
System.Windows.Controls.ComboBoxItem currentItem=((System.Windows.Controls.ComboBoxItem)ddDeputi.SelectedItem);
string myvalue=currentItem.Content;
Here myvalue gives u the selected value.
Upvotes: 0
Reputation: 8503
When you bind to ADO.NET you are always data-binding to a DataView. Each DataRowView wraps a DataRow in the source DataTable
You can get the selected DataRow via the following:
DataRowView selectedDataRowView = (DataRowView)ddDir.SelectedItem;
DataRow selectedRow = selectedDataRowView.Row;
Upvotes: 2