Reputation: 29
I'm trying to get the values in a table's column to populate the options of a combobox. Whenever I try, the combobox fills with the correct amount of options, but instead of the column's values, the options are all 'System.Data.DataRowView'.
XAML
<ComboBox x:Name="ComboBox_1" HorizontalAlignment="Left" Margin="124,23,0,0" VerticalAlignment="Top" Width="95" Height="42" ItemsSource="{Binding}"/>
C#
public MainWindow()
{
InitializeComponent();
string connectionString = "SERVER=localhost;DATABASE=dbname; UID=myPC;Password=myPW;";
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand cmd = new MySqlCommand("Select columnname from tablename", connection);
connection.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
connection.Close();
ComboBox_1.DataContext = dt;
}
Upvotes: 0
Views: 619
Reputation: 2044
You should set the DisplayMemberPath like this.
<ComboBox DisplayMemberPath="YourColumnName"/>
And set ComboBox's ItemsSource as dt.DefaultView like this.
ComboBox_1.ItemsSource = dt.DefaultView;
You can show only one column in the ComboBox. If you want to show all the columns, you can use a DataGrid instead or define an ItemTemplate for ComboBox.
Upvotes: 1