Filter combobox in a datgrid based on another combobox vb.net
I think my question is discriptive, or as Microsoft in the Documentation for Data Grid the question is, How do I have a combo box column display a sub set of data based upon the value of a different combo box column?
I have a DS with 3 tables filled, Customers, Orders, OrderDetails. The order details is in a DataGridView with two combo boxes columns, 1 is the Location, and 1 the products. Both come from seperate Lookup Datasets.
What I want is that when the user selects the Location combo , the Products combo should be filtered to the locations its availiable. The Products is related to location by the LocationID
This is the solution from the documentation but it dosent work for me.
private void Form1_Load(object sender, EventArgs e)
{ this.territoriesTableAdapter.Fill(this.northwindDataSet.Territories); this.regionTableAdapter.Fill(this.northwindDataSet.Region);
// Setup BindingSource for filtered view.
filteredTerritoriesBS = new BindingSource();
Da开发者_如何学JAVAtaView dv = new DataView(northwindDataSet.Tables["Territories"]);
filteredTerritoriesBS.DataSource = dv;
}
private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e) { if (e.ColumnIndex == territoryComboBoxColumn.Index) { // Set the combobox cell datasource to the filtered BindingSource DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1 [e.ColumnIndex, e.RowIndex]; dgcb.DataSource = filteredTerritoriesBS;
// Filter the BindingSource based upon the region selected
this.filteredTerritoriesBS.Filter = "RegionID = " +
this.dataGridView1[e.ColumnIndex - 1, e.RowIndex].Value.ToString();
}
}
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == this.territoryComboBoxColumn.Index) { // Reset combobox cell to the unfiltered BindingSource DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView1 [e.ColumnIndex, e.RowIndex]; dgcb.DataSource = territoriesBindingSource; //unfiltered
this.filteredTerritoriesBS.RemoveFilter();
}
}
I found a soulution provided by vb-tips.com It seems to work great and filters the DGV upon loading the form here is my code; http://codepaste.net/wra8qw
精彩评论