10

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

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 No and Yes and the values as 0 and 1 respectively. What I want to get here's the text Yes or 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.

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());
palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • Check this [**`Link`**](http://stackoverflow.com/a/6051032/1577396) and reply me.. – Mr_Green Nov 16 '12 at 14:57
  • As @Mr_Green said - it's not clear what you're looking for. Can you edit the question to say what your expected and actual results are? – Bobson Nov 16 '12 at 15:25

5 Answers5

17

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

string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);
Rahul Rajput
  • 1,427
  • 3
  • 17
  • 38
  • I don't think you actually need to `Convert.ToString()` in the first case, since you're calling `.ToString()` on the FormattedValue. Very useful, though. – Bobson Nov 23 '12 at 16:43
  • @Bobson Convert.ToString() handles the null value and if i write only .Tostring() and there is null value it will raise exception at runtime. :) – Rahul Rajput Nov 27 '12 at 11:30
  • 1
    That's a really good point. But does that then mean that the `.ToString()` is the redundant bit? – Bobson Nov 27 '12 at 14:21
4

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.

WozzeC
  • 2,630
  • 1
  • 13
  • 12
  • Thanks @Wozzec for the reply but the solution didn't work for me as I wanted. I have Yes and No as the TEXT in the combobox. I was able to get the TEXT `Yes` but when I changed the combobox value to `No`, it was still showing the value as `Yes`, where it should have been `No` as the selected item TEXT. I hope you got what my problem here is. – palaѕн Nov 19 '12 at 09:04
  • How do you add your items to the combobox? I created a class containing variables text and value. I then set the combobox column to use a bindingsource with a list of the textvalue class as source. So for me i get the TEXT "No" when I changed combobox item. – WozzeC Nov 19 '12 at 11:01
  • I am adding dynamically combobox column to the grid and loading with the data in the DataTable as list of itmes. – palaѕн Nov 19 '12 at 11:05
  • For me this works as intended. The solution with just "FormatedValue".ToString() (as Rahul Rajput suggests) always gives an empty string though. I populated my combobox dynamically with "_Combobox.DataSource = list;". "list" is a string list. – Birk Mar 08 '17 at 15:55
1

Also try this:

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

EditedFormattedValue object 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 ComboBox selection or any value of cell in DataGridView.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
Osama Rizwan
  • 615
  • 1
  • 7
  • 19
0

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

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.

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:

    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 Text property 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.

Paul Murphy
  • 565
  • 1
  • 5
  • 11
-1

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.

Derek
  • 8,300
  • 12
  • 56
  • 88
  • That doesn't provide anything different than what the OP has - he already can get the correct cell's `Value`. – Bobson Nov 16 '12 at 15:06
  • Yes @Bobson you are absolutely right. I am already getting the correct cell's value. I just need to get the `combobox selected text`. – palaѕн Nov 19 '12 at 06:28