1)建立的BindingSource兩個表某些SQL的方法。
BindingSource bsGroup = new BindingSource();
BindingSource bsUser = new BindingSource();
bsGroup.DataSource = MyDataSet.Tables["Group"];
bsUser.DataSource = MyDataSet.Tables["User"];
2)設置Combo和Grid DataSources。
MyCombo.DataSource = bsGroup;
MyCombo.DisplayMember = "GroupName"; // whatever your ColumnName is
MyCombo.ValueMember = "GroupID";
MyGrid.DataSource = bsUser;
3)建立SelectedIndexChanged事件的組合,並用它來改變在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;
}
這工作正常......取自here。
(是的,我張貼這也是在MSDN上,因爲我很着急)
UPDATE1:我研究多了,發現過濾選項...詮釋選擇= int.Parse(myCombo.SelectedValue.ToString( )); userBindingSource1.Filter =「groupId =」+ selected; – Enrique 2009-12-03 13:41:44
Update2:雖然過濾器工作正常,我想添加dataGridView中的用戶,他們相應的選擇groupId - 從組合,並保持過濾也......任何線索? – Enrique 2009-12-03 13:42:49