Reputation: 67
I have a DataTable, and a ComboBox gets items from this table as:
SQLiteDataAdapter sQLiteDataAdapter = new SQLiteDataAdapter(_command);
DataTable dataTable = new DataTable("Alarm");
sQLiteDataAdapter.Fill(dataTable);
cboxAlarmFilterSeverity.DisplayMemberPath = "severity";
cboxAlarmFilterSeverity.SelectedValuePath = "severity";
cboxAlarmFilterSeverity.ItemsSource = dataTable.AsDataView();
I want to get only distinct values to ComboBox. I know, I could use this query, but I do not want to use:
_command.CommandText = "SELECT DISTINCT severity FROM [Alarm]";
And I could use this solution, but it should not interact with XAML, it should be programmatically.
How can I do that?
Upvotes: 1
Views: 153
Reputation: 169200
The DataView
has a ToTable
method that creates a new DataTable
that contains rows that have distinct values:
cboxAlarmFilterSeverity.ItemsSource = dataTable.AsDataView()
.ToTable(true, "severity")
.DefaultView;
Upvotes: 1