C# 如何获取 Datagridview Combobox 所选项目的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13418354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:30:36  来源:igfitidea点击:

How to get the TEXT of Datagridview Combobox selected item?

c#winformsdatagridviewcomboboxselectedtext

提问by pala?н

How to get the Combobox selected item textwhich is inside a DataGridView? I have tried using the below code:

如何获取DataGridView内的组合框选定项文本?我尝试使用以下代码:

dataGridView1.Rows[1].Cells[1].Value.ToString()

But, this gives the value associated with this cell, not the Combobox selected item text. I also tried this:

但是,这给出了与此单元格关联的值,而不是组合框选定项文本。我也试过这个:

DataGridViewComboBoxCell cell = dataGridView1[1,1] as DataGridViewComboBoxCell;
string value = cell.Value.ToString();

But, this also didn't help.

但是,这也没有帮助。

I would appreciate your help. Thanks in advance!

我会很感激你的帮助。提前致谢!

EDIT:

编辑:

Let's say, we have a Combobox with text as Noand Yesand the values as 0 and 1 respectively. What I want to get here's the text Yesor No, when the Combobox is changed. But what I am getting is the values 0/1 using the above codes. Hope that makes things clear.

比方说,我们有一个组合框,文本为NoYes,值分别为 0 和 1。我想在这里得到的是文本Yesor No,当组合框改变时。但我得到的是使用上述代码的值 0/1。希望这能让事情变得清楚。

UPDATE:

更新:

Ok, so I have been working on this issue and after lots of efforts and with help from my fellow members, I have been able to resolve the issue and get the required solution:

好的,所以我一直在研究这个问题,经过大量努力并在我的同事的帮助下,我已经能够解决问题并获得所需的解决方案:

Here's the solution:

这是解决方案:

string SelectedText = Convert.ToString(dataGridView1.Rows[0].Cells[1].FormattedValue.ToString());

采纳答案by Rahul Rajput

To get selected value and selected text of Combobox in DataGridView try following Code

要在 DataGridView 中获取组合框的选定值和选定文本,请尝试以下代码

string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);

回答by Derek

You could Try this :-

你可以试试这个:-

dataGridView1.CurrentRow.Cells[0].Value.ToString();

Index the column of the cell you need to look at, whichever is the index of your ComboBoxColumn.

索引您需要查看的单元格的列,以您的ComboBoxColumn.

回答by WozzeC

I managed to pull that string value out of the cell this way:

我设法通过这种方式将该字符串值从单元格中拉出:

DataGridViewComboBoxCell dgvcmbcell = dataGridView1[1, 0] as DataGridViewComboBoxCell;
String text = dgvcmbcell.EditedFormattedValue.ToString();

Easiest way to figure this out is use the debugger and look into the dgvcmdcell object. In this you will find the expandable node "base". Expand it and just look through it and you will find whatever information you need.

解决这个问题的最简单方法是使用调试器并查看 dgvcmdcell 对象。在此您将找到可扩展节点“base”。展开它并浏览它,您将找到所需的任何信息。

回答by Paul Murphy

To access the currently selected text in the datagridview, you need a reference to the CurrencyManagerof the Combobox column. The CurrencyManagerhas nothing to do with money but instead manages the binding between the the column and it's datasource. The CurrencyManagercan tell you what the currentselection of the combobox is.

要访问 datagridview 中当前选定的文本,您需要引用Combobox 列的CurrencyManager。该CurrencyManager的无关金钱,而是管理列,它的数据源之间的绑定。该CurrencyManager的可以告诉你的是什么当前的组合框的选择。

Teh codes:

代码:

    CurrencyManager cm = (CurrencyManager)DataGridView1.BindingContext[((System.Windows.Forms.DataGridViewComboBoxColumn)DataGridView1.Columns[0]).DataSource];

Note:it is not necessary to cast the column to a combobox, i just did that to show you what column I was passing in. I used index 0 but use whatever index is the actual index of your combobox column.

注意:没有必要将列转换为组合框,我只是这样做是为了向您显示我传入的列。我使用索引 0 但使用任何索引是组合框列的实际索引。

Now using the currency manager you can access the current selection of the datagrid for that column (because that was the column you passed in).

现在使用货币管理器,您可以访问该列的数据网格的当前选择(因为这是您传入的列)。

    cm.Current; //returns the current selection whatever that is

So in my case the datasource of the combobox column was a class called Choice, choice looks like this:

所以在我的例子中,组合框列的数据源是一个名为 Choice 的类,choice 如下所示:

    public class Choice
    {
            public string Text
            {
                get;
                set;
            }
    }

When I access the cm.Current property it will return an instance of the choice class, I can now access the Textproperty of my choice class to see what value was selected. You will obviously have to adapt this to your particular situation. I hope this helps.

当我访问 cm.Current 属性时,它将返回选择类的一个实例,我现在可以访问我的选择类的Text属性以查看选择了什么值。显然,您必须根据您的特定情况进行调整。我希望这有帮助。

回答by Osama Rizwan

Also try this:

也试试这个:

DataGridView.CurrentRow.Cells(Column.name).EditedFormattedValue.ToString()

EditedFormattedValueobject gives the current, formatted value of the cell in DataGridView, regardless of the cell is edited or in edit mode. It's more convenient to capture the ComboBoxselection or any value of cell in DataGridView.

EditedFormattedValue对象在 中给出单元格的当前格式化值DataGridView,而不管单元格是否被编辑或处于编辑模式。这是更方便地捕捉ComboBox选择或细胞的任何值DataGridView