Create Master/Detail with Combobox and DataGridView
I have a ComboBox with its elements loaded from a sqlserver table (groups). I also have a DataGridView, linked to another table (users).
When I load the form, the combo is filled with groups, and the datagridview is filled with users (all the users)
1) I want to be able to select a group from the ComboBox (dropDownList), and then to refresh the DataGridView with the users that belong to that selected group...
2) And How can I show a join relation in the DataGridView? Let's say I want to show the groupName in the last column of the each us开发者_JAVA技巧er...
PS:
I have an xsd Dataset created in my VS2008 project with its corresponding tableAdapters generated (groupTableAdapter, userTableAdapter) and some sql-methods added to each adapter
1) Set up a BindingSource for both tables.
BindingSource bsGroup = new BindingSource();
BindingSource bsUser = new BindingSource();
bsGroup.DataSource = MyDataSet.Tables["Group"];
bsUser.DataSource = MyDataSet.Tables["User"];
2) Set up your Combo and Grid DataSources.
MyCombo.DataSource = bsGroup;
MyCombo.DisplayMember = "GroupName"; // whatever your ColumnName is
MyCombo.ValueMember = "GroupID";
MyGrid.DataSource = bsUser;
3) Set up a SelectedIndexChanged event for the Combo and use it to change the filter on the bsUser bindingsource.
MyCombo.SelectedIndexChanged += new System.EventHandler(MyCombo_SelectedIndexChanged);
private void MyCombo_SelectedIndexChanged(object sender, System.EventArgs e)
{
// this will depend on what your column names are, obviously
string filter = string.Format("GroupID = {0}", MyCombo.SelectedValue);
bsUser.Filter = filter;
}
This worked fine... taken from here.
(Yes, I posted this also on MSDN because I was in a hurry)
精彩评论