使用 Combobox 和 DataGridView 创建主/详细信息

发布于 2024-08-13 04:32:00 字数 458 浏览 3 评论 0原文

我有一个 ComboBox,其元素从 sqlserver 表(组)加载。 我还有一个 DataGridView,链接到另一个表(用户)。

当我加载表单时,组合填充了组,并且 datagridview 填充了用户(所有用户)

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

2) 以及如何在 DataGridView 中显示联接关系?假设我想在每个用户的最后一列中显示组名...

PS:

我在 VS2008 项目中创建了一个 xsd 数据集,其 生成对应的tableAdapter (组表适配器、用户表适配器) 并添加了一些 sql 方法 适配器

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 user...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘦怹 2024-08-20 04:32:00

1) 为两个表设置 BindingSource。

BindingSource bsGroup = new BindingSource();
BindingSource bsUser  = new BindingSource();
bsGroup.DataSource = MyDataSet.Tables["Group"];
bsUser.DataSource = MyDataSet.Tables["User"];

2) 设置组合和网格数据源。

MyCombo.DataSource    = bsGroup;
MyCombo.DisplayMember = "GroupName"; // whatever your ColumnName is
MyCombo.ValueMember   = "GroupID";
MyGrid.DataSource = bsUser;

3) 为 Combo 设置 SelectedIndexChanged 事件,并使用它来更改 bsUser 绑定源上的过滤器。

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;
}

这工作正常...取自 此处

(是的,我也在 MSDN 上发布了这个,因为我很着急)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文