DataGridView中的动态ComboBox [英] Dynamic ComboBox in a DataGridView

查看:240
本文介绍了DataGridView中的动态ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView有4列,其中一列是一个下拉列表(ComboBox)。此列中的值必须不同,因此一旦在一行中选择了一个给定的下拉列表值,我希望它在其他行中的任何下拉列表中不可用。如果给定的值被取消选择,它将再次显示在下拉列表中。我发现了一些类似示例的文章( 1 2 3 );然而,他们似乎都涉及订阅一个事件并手动填充ComboBox。




  • 有没有办法用数据绑定来完成这个(可能是一个IEnumerable,基本上是一个 all.Except(selected) Linq query)?

  • 如果单独的数据绑定还不够,有没有比我发现的例子更好的方法?



我对WinForms开发有一点新意有麻烦的是从是的,这将会起作用中分开好的建议。

解决方案

我恐怕你的搜索到目前为止是正确的 - 没有更好的方法来提供不同的列表到组合框在同一个DataGridView列比订阅事件(通常 CellBeginEdit CellEndEdit 然后为单个 DataGridViewComboBoxCell 设置DataSource。



。DataGridView常见问题的样例/ Go%2520至%2520Market / DataGridView / DataGridView%2520FAQ.doc& ei = anuLTtL4GMjHtAbVotyTAg& usg = AFQjCNFxVe3EWtsJNP7xXxff6dxU0EQrel =nofollow> DataGridView FAQ 。常见问题解答与您发布的链接非常相​​似。


I have a DataGridView with 4 columns, one of them being a drop-down (ComboBox) column. The values in this column must be distinct so once a given drop-down values is selected in a row, I'd like it to not be available in any of the drop-downs in other rows. If a given value is unselected it should show up in the drop-downs again. I found a few articles with similar examples (1,2,3); however, all of them seem to involve subscribing to an event and manually populating the ComboBox.

  • Is there a way to accomplish this with databinding alone (maybe to an IEnumerable that's basically a all.Except(selected) Linq query)?
  • If databinding alone isn't enough, is there a better way to do it than the examples I found?

I'm a bit new to WinForms development so having trouble telling apart good advice from "yeah...that'll work, sort of".

解决方案

I'm afraid your searching so far is correct - there is no better way to provide different lists to comboboxes in the same DataGridView column than subscribing to events (usually CellBeginEdit and CellEndEdit and then setting the DataSource for an individual DataGridViewComboBoxCell).

By way of evidence of this, I'll point you to the excellent DataGridView FAQ which was written by Mark Rideout, the DataGridView program manager at Microsoft. The FAQ has a full solution to your problem which is very similar to the links you post.

这篇关于DataGridView中的动态ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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