C# Datagridview:获取组合框列中的选定项 [英] C# Datagridview: get selected item in combobox columns

查看:23
本文介绍了C# Datagridview:获取组合框列中的选定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个允许用户操作 xml 文件的 GUI.我在通过 xml 元素按列整齐组织的 datagridview 中显示 xml 文件.我允许用户在我的项目中添加列作为扩展.该列被添加到数据集表中,然后更新到我用来显示 xml 文件的 datagridveiew.我已经包含了用户添加组合框列来选择选项而不是不断输入它们的功能,例如..对或错.然而,这就是问题所在.保存普通列很容易.组合框列很麻烦.

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.

我有一个保存组合框列"可以将其更新为 xml,还有一个保存"按钮可以保存到用户选择的目的地.

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;

我尝试将其应用于组合框列类,但它没有这样的功能.因此,我无法弄清楚如何直接获取用户所选项目的值.我也尝试使用 comboboxcell 进行试验,但这也没有使我取得任何进展.我玩过的这两个课程都没有......选定项目"功能,甚至谷歌也没有适合我的解决方案.=( 我也试过使用 cell.value,但由于某种原因它是null".即使用户选择框中的一个项目,它也不会被保存到单元格的值中.

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:简而言之,我的问题是,如果可能,您如何访问组合框列单元格的所选项目?此外,您将如何确保项目值保存在单元格中?

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?

提前致谢.我正在使用 .NET 3.5 SP1,通过 Visual Studio 2008 C#.

Thanks in advance. I'm using .NET 3.5 SP1, through Visual Studio 2008 C#.

真诚的

tf.rz

推荐答案

DataGridView 中的 Control 不是 ComboBox,它是 DataGridViewComboBox 并且具有不同的属性和方法.来自 MSDN

The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN

与 ComboBox 控件不同,DataGridViewComboBoxCell 没有 SelectedIndex 和 SelectedValue 属性.相反,从下拉列表中选择一个值会设置单元格的 Value 属性.

Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.

但是,您提到 Cell.Value 对您来说是 null.根据以下文章(如何:访问 Windows 窗体 DataGridViewComboBoxCell 下拉列表中的对象).

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

您必须将 DataGridViewComboBoxColumn.ValueMember 或 DataGridViewComboBoxCell.ValueMember 属性设置为业务对象上的属性名称.当用户进行选择时,业务对象的指示属性设置单元格值属性.

You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.

这篇关于C# Datagridview:获取组合框列中的选定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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