0

我有一个ComboBox,其元素从sqlserver表(组)中加载。 我也有一个DataGridView,链接到另一个表(用户)。使用Combobox和DataGridView创建大师/详细信息

当我加载的形式中,组合填充有基团,和在DataGridView填充有用户(所有用户)

1)我希望能够从ComboBox选择一组( dropDownList),然后用属于该选定组的用户刷新DataGridView ...

2)以及如何在DataGridView中显示连接关系?比方说,我要显示在每个用户的最后一列的组名...

PS:

我在VS2008的项目,其 相应的TableAdapter创建一个xsd数据集生成 (groupTableAdapter,userTableAdapter) ,并添加到每个 适配器

+0

UPDATE1:我研究多了,发现过滤选项...诠释选择= int.Parse(myCombo.SelectedValue.ToString( )); userBindingSource1.Filter =“groupId =”+ selected; – Enrique 2009-12-03 13:41:44

+0

Update2:虽然过滤器工作正常,我想添加dataGridView中的用户,他们相应的选择groupId - 从组合,并保持过滤也......任何线索? – Enrique 2009-12-03 13:42:49

回答

3

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上,因为我很着急)

+0

MSDN链接是http://social.msdn.microsoft.com/Forums/windows/en-US/3fba63f0-f027-48e9-b9a5-4738c620a5fb/c-master-detail-using-combobox-and-datagridview-with -tableadapters – 2013-09-07 02:04:56