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

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

问题描述

我正在使用一个GUI,允许用户操纵xml文件。我在一个由通过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.

我做了一些研究,看起来像combobox类有这样的功能,以获得访问的选择项在组合框中由用户放入。
我们有:

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;



我尝试将此应用于combobox列类,但没有这样的函数。因此,我不知道如何直接获得用户选择的项目的值。我试着使用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:
我的问题是,如果可能的话,你是否可以访问combobox列的单元格选择的项目?此外,如何确保项目值保存在单元格中?

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中的控件不是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。根据以下文章 http: //msdn.microsoft.com/en-us/library/ms404353(v=VS.90).aspx

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 http://msdn.microsoft.com/en-us/library/ms404353(v=VS.90).aspx


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

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天全站免登陆