C# Datagridview:获取组合框列中的选定项目
我正在开发一个允许用户操作 xml 文件的 GUI。我在 datagridview 中显示 xml 文件,通过 xml 元素按列整齐地组织。我允许用户添加列作为我的项目的扩展。该列被添加到数据集表中,然后更新到我用来显示 xml 文件的 datagridveiew。我已经包含了用户添加组合框列来选择选项的功能,而不是像这样不断地输入它们。是真是假。然而,这正是问题所在。保存普通色谱柱很容易。组合框列很痛苦。
我有一个“保存组合框列”,可将其更新为 xml,还有一个“保存”按钮,可保存在用户选择的目的地中。
我做了一些研究,似乎组合框类具有这样的功能,可以访问用户输入的组合框中的选定项目。 我们有:
ComboBox box = new ComboBox();
box.SelectedItem;
我尝试将其应用于组合框列类,但它没有这样的功能。因此,我无法弄清楚如何直接获取用户所选项目的值。我也尝试过使用组合框单元进行实验,但这也没有使我有任何结果。我玩过的这两个类都没有...“所选项目”功能,甚至谷歌也没有为我提供解决方案。 =( 我也尝试过使用 cell.value,但由于某种原因它是“null”。即使用户在框中选择一个项目,它也不会保存到单元格的值中。TLDR
: 简而言之,我的问题是,如果可能的话,如何访问组合框列单元格的选定项目?此外,如何确保项目值保存在单元格中?
提前致谢。我通过 Visual Studio 2008 C# 使用 .NET 3.5 SP1。
此致,
tf.rz
I'm working on a GUI that allows the user to manipulate xml files. I display the xml file in a datagridview organized neatly by columns through xml elements. I allow the user to add columns as an extention on my project. The column gets added to the dataset table, then updated to the datagridveiew that I use to display the xml file in. I've included the ability for the user to add a combobox column to select choices instead of entering them in constantly like.. true or false. However, that is where the problem lies. Saving a normal column was easy. The combobox column is being a pain.
I have a "save combobox column" to have it updated to the xml and a "save" button to save in a destination of the user's choice.
I've done some research and it seems like the combobox class has such a feature to gain access to the selecteditem in the combobox put in by the user.
Where we have:
ComboBox box = new ComboBox();
box.SelectedItem;
I tried applying this to the combobox column class but it does not have such a function. Thus, I cannot figure out how to directly obtain the value of the user's selected item. I tried experimenting with comboboxcell's as well, but that didn't lead me anywhere either. Both those classes I played around with do not have a... "selected item" function and even google does not have a solution for me. =( I've also tried using the cell.value, but it is "null" for some reason. Even when the user selects an item in the box, it doesn't get saved into the cell's value.
TLDR:
My question in short is, how, if possible, do you gain access to the comboboxcolumn cell's selected item? Additionally, how would you then ensure that the item value is saved in the cell?
Thanks in advance. I'm using .NET 3.5 SP1, through Visual Studio 2008 C#.
Sincerely,
tf.rz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
DataGridView 中的控件不是 ComboBox,它是 DataGridViewComboBox,并且具有不同的属性和方法。来自 MSDN
但是,您提到 Cell.Value 对您来说为空。好吧,根据以下文章,您可能还缺少另一个步骤(如何:访问 Windows 窗体 DataGridViewComboBoxCell 下拉列表中的对象)。
The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN
However, you mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article (How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List).
如果我们将
datagridcomboboxcell
与不同的DisplayMember
和ValueMember
绑定,如下所示:然后获取
SelectedText
,并且SelectedValue
,我们可以编写这段代码:希望它能解决您的问题。
If we have bound a
datagridcomboboxcell
with a differentDisplayMember
andValueMember
, like so:Then for getting
SelectedText
, andSelectedValue
, we can write this code:I hope it solves your problem.
使用它来获取或设置选定的值:
不要忘记为 DataGridViewComboBoxColumn 设置
DisplayMember
和ValueMember
Use this to get or set selected value:
Don't forget to set
DisplayMember
andValueMember
for your DataGridViewComboBoxColumn是这样完成的
This is how it is done
我用这个:
I use this:
.Net 组合框实际上是由文本框和下拉列表组成的复合控件。使用
box.Text
获取当前显示的信息。编辑:行或单元格应该有一个 .FindControl() 方法。您需要执行以下操作:
Combobox box = (Combobox)(row.FindControl("[combobox ID]"));
string val = box.Text;
基本上,您在其容器(行或单元格)中查找控件,然后将找到的控件转换为组合框,然后访问其 .Text 属性。
A .Net combox is actually a composite control made up of a textbox and a dropdownlist. Use
box.Text
to get the currently displayed information.EDIT: The row or the cell should have a .FindControl() method. You'll need to do something like:
Combobox box = (Combobox)(row.FindControl("[combobox ID]"));
string val = box.Text;
Basically, you're finding the control within its container (row or cell), then casting the control found as a combobox, then accessing its .Text property.
我的首选解决方案是将控件转换为普通组合框,之后 selectitem、selectedvalue 等都可用。在 vb.net 中,其中 dgvSchedule 是我的 datagridview:
My preferred solution is to cast the control as an ordinary combo box, after which selecteditem, selectedvalue, etc. are all available. In vb.net, where dgvSchedule is my datagridview: