C# - 在设计时设置数据绑定级联组合框 [英] C# - Setting up data-bound cascading combo boxes at design time

查看:192
本文介绍了C# - 在设计时设置数据绑定级联组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么我认为是一个简单的问题,但经过多次搜索后找不到一个合适的例子。
简单地说,它是一个窗体应用程序,在表单上有2个数据绑定的组合框,第一个是部门,第二个是部门。这些组合都绑定到SQL数据库中的相应表。组合应该与用户从第一个选择部门操作,导致第二个组合被过滤,只显示属于该部门的部分。 (换句话说这是经典的级联组合框问题)。
我的经验最初是与Delphi,这是一个简单的设计时间问题。

I have what I thought would be a simple problem but cannot find an appropriate example after much searching. Put simply, it is a windows form application with 2 databound combo boxes on the form with the first being for "Department" and the second for "Section". These combos are each bound to the corresponding tables in a SQL database. The combos should operate with the user selecting Department from the first which causes the second combo to be filtered to show only sections belonging to that department. (In other words this is the classic "cascading" combo boxes problem). My experience is originally with Delphi and this was a simple design time issue.

但是,在C#我被困,只是不能得到它的工作。我本来以为这可以在设计时完成,也许在SelectedIndexChanged事件背后的一些最小的代码。如果任何人有一个基本(形式,2个组合框,简单的数据源)的例子他们可以指向我。非常感谢。

However, in C# I am stuck and just can't get it to work. I would have thought that this could be done at design time with maybe some minimal code behind the SelectedIndexChanged event. I would be most grateful if anyone has a basic (form, 2 comboboxes, simple data sources) example of this they can point me to.

谢谢,詹姆斯

推荐答案

ComboBox cbDepartment = new ComboBox();
cbDepartment.Name = "cbDepartment";
cbDepartment.DataSource = dsDepartments;
cbDepartment.SelectedIndexChanged = new System.EventHandler(cbDepartment_SelectedIndexChanged);

private void cbDepartment_SelectedIndexChanged(object sender, System.EventArgs e) {
    cbSection.DataSource = GetSection(cbDepartment.SelectedItem.Value);
}

您可以尝试上述操作。

这篇关于C# - 在设计时设置数据绑定级联组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆