C# - 在设计时设置数据绑定级联组合框
我有一个我认为是一个简单的问题,但经过多次搜索后找不到合适的例子。 简而言之,它是一个 Windows 窗体应用程序,窗体上有 2 个数据绑定组合框,第一个用于“部门”,第二个用于“部分”。这些组合均绑定到 SQL 数据库中的相应表。组合应该与用户从第一个组合中选择“部门”一起操作,这会导致第二个组合被过滤以仅显示属于该部门的部分。 (换句话说,这是经典的“级联”组合框问题)。 我的经验最初是使用 Delphi,这是一个简单的设计时间问题。
然而,在 C# 中我陷入困境,无法让它工作。我本以为这可以在设计时通过 SelectedIndexChanged 事件背后的一些最少代码来完成。如果有人有一个基本的(表单、2 个组合框、简单数据源)示例,他们可以向我指出,我将不胜感激。
谢谢,詹姆斯
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.
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.
Thanks, James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以尝试类似上面的方法。
You can try something like the above.
您没有指定要绑定的内容。我总是绑定到对象,所以这是我的做法:
在表单加载事件中:
以及父组合框的事件处理程序:
我不知道在设计时有什么方法可以做到这一点。
您可能应该重写绑定到下拉列表的对象的 ToString() 方法,以控制组合框中显示的文本。
You didn't specify what you were binding to. I always bind to objects, so here is how I do it:
In the form load event:
And the event handler for the parent combobox:
I don't know any way to do it at design time.
You should probably override the ToString() method of the objects bound to the drop down list to control the text that is displayed in the combobox.