Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the TEXT of Datagridview Combobox selected item?

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());
like image 674
palaѕн Avatar asked Nov 16 '12 14:11

palaѕн


2 Answers

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);
like image 58
Rahul Rajput Avatar answered Sep 24 '22 03:09

Rahul Rajput


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.

like image 27
WozzeC Avatar answered Sep 22 '22 03:09

WozzeC