A novice question if I may.
I'm trying to cascade filter comboboxes to DGV in my winforms project.
I have a list derived from a class that populates cbx1 with CategoryA items, I'm trying to populate cbx2 with CategoryB items in the SelectedIndexChanged event based on the choice in cbx1 and displaying only the relevant categories in cbx2. It works as expected but without the filter.
The commented out LINQ is an attempt to do this but returns errors.
A point in the right direction would be great, thanks.
I'm trying to cascade filter comboboxes to DGV in my winforms project.
I have a list derived from a class that populates cbx1 with CategoryA items, I'm trying to populate cbx2 with CategoryB items in the SelectedIndexChanged event based on the choice in cbx1 and displaying only the relevant categories in cbx2. It works as expected but without the filter.
The commented out LINQ is an attempt to do this but returns errors.
A point in the right direction would be great, thanks.
C#:
void cbxCategoryAFill()
{
List<Screwfix> scr = sc.GetAllScrewfixResults();
this.comboBox1.DataSource = scr.Select(t => t.CategoryA).Distinct().ToList();
comboBox1.Text = "--Select--";
}
void cbxCategoryBFill()
{
List<Screwfix> scr = sc.GetAllScrewfixResults();
this.comboBox2.DataSource = scr.Select(t => t.CategoryB).Distinct().ToList();
comboBox2.Text = "--Select--";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate { comboBox1.Text = "-Select-"; });//Note Resets combobox after selection
List<Screwfix> scr = sc.GetAllScrewfixResults();
dgvLeft.DataSource = scr.Where(x => x.CategoryA.Contains(comboBox1.Text)).ToList();
// comboBox2.DataSource = ((IEnumerable<string>)dgvLeft.DataSource)
//.Where(x =>
//{
// return x == (string)comboBox1.SelectedValue;
//});
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate { comboBox1.Text = "-Select-"; });
List<Screwfix> scr = sc.GetAllScrewfixResults();
dgvLeft.DataSource = scr.Where(x => x.CategoryB.Contains(comboBox2.Text)).ToList();
}