This seems to be a straight forward thing, but Microsoft only knows why this simple feature is not supported in DataGridView.
If you have a ComboBoxColumn in your DataGridView and you want to know what is the selected index of the combo box, then you need to do this:
1. Handle the EditingControlShowing event of DataGridView. In this event handler, check if the current column is of our interest. Then we create a temporary ComboBox object and get the selected index:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { if (dataGridView1.CurrentCell.ColumnIndex == 0) { // Check box column ComboBox comboBox = e.Control as ComboBox; comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged); } } void comboBox_SelectedIndexChanged(object sender, EventArgs e) { int selectedIndex = ((ComboBox)sender).SelectedIndex; MessageBox.Show("Selected Index = " + selectedIndex); }
A rather round-about solution where a simple one would have sufficed.
You can do it using:
((DataGridView)sender).CurrentCell.RowIndex
Great help
Excellent Job!!! . Keep It Up . God Bless u with more nurturing capabilities so that programmers like us keeps on geetting tips from u…………….. Thank u…………………………
Nice piece of code! Thanks
Thanks man,
It’s exactly what i want.
thanks man..
really helped..
Its working thanks
Thanks a lot , I was struggling for 1 day for this
Thanks dude…. This code is helped me lot…
thank u very much dear …..i got lot of help from this effective code
Hi,
Thanks for the “life saving” work around.
I am using it further to enable a button which is outside of grid depending
on the value selected in ComboBoxCell.
The code below works well if I change the values of only one ComboBox, but
when I try to change values in multiple it fails.
e.g. to disable the Save button I have to make all ComboBox values = “Select”
which is default value, even after that IsItemSelected method returns “true”
After inserting a break point at
>> if (cell.Value.ToString() != “Select”)
I still get the last value in Cell which is other than “Select”
void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = ((ComboBox)sender).SelectedIndex;
//MessageBox.Show(“Selected Index = ” + selectedIndex);
if (selectedIndex > 0)
{
btnSave.Enabled = true;
}
else
{
// check for all rows
btnSave.Enabled = (IsItemSelected() == true ? true : false);
}
}
private bool IsItemSelected()
{
bool isItemSelected = false;
foreach (DataGridViewRow dgvRows in dgvPending.Rows)
{
DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dgvRows.Cells[0];
if (cell != null)
{
if (cell.Value.ToString() != “Select”)
{
isItemSelected = true;
break;
}
}
}
return isItemSelected;
}
Please guide
Thanks in advance
You forgot to remove an existing event- handler, if present
Just look at
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridvieweditingcontrolshowingeventargs.control.aspx
thanks a lot. I am using a DataGridViewComboBoxCell. Now with ur solution i am able to get the selectedindex but can i delete this control(comboboxcell) and get a textboxcell back on to the cell.
Nice one…
it really helps me….
Just Beautiful
Good one… Thanks a lot
OOhhhhhhhh……
What A Solution!!!!!
Realy Helps Lottt…
Thanks Lottt…
It is really help me move next step of my project. Thanks a lot and keep up good work. Thanks a lot.
Thanks a lot.
God bless you.
Dear Sir,
I am using Datagridview in my new billing project where in i am opting DatagridviewComboboxColoumn to populate the items ( e.g. Packing Charges, Discount, Sales Tax,Freight etc).
I am able to populate the items in combobox but are not able to get seleceted value of combobox event and fill up the other DatagridviewTextboxes on that selected value.
Can anybody help me please.
Dear sir
your code add new combobox with new data but i have a question i want to get selected index of existing datagridveiwcombobox in datagridview …if you have any idea?
pls help me!!!!!!!!!
Awesome. Thanks!!
Work fine for me with one change. I had to remove event handler after invoke event like this:
12 void comboBox_SelectedIndexChanged(object sender, EventArgs e)
13 {
14 int selectedIndex = ((ComboBox)sender).SelectedIndex;
15 MessageBox.Show(“Selected Index = ” + selectedIndex);
comboBox.SelectedIndexChanged -= new EventHandler(comboBox_SelectedIndexChanged);
16 }
Sorry mistake , should be:
((ComboBox)sender).SelectedIndexChanged-=EventHandler(comboBox_SelectedIndexChanged);
Awesome:)
I had this code:
And in the code behind I added:
private void DocTypeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = documentFilesDataGrid.SelectedItem;
if (item != null)
{
int selectedIndex = ((ComboBox)sender).SelectedIndex;
currentSession.fileList[0].Doc_Type = docTypes[selectedIndex].ToString();
DebugLabel.Content = selectedIndex;
}
}
Works like charm!:D:D Awesome!
thanks a lot ,It’s so useful
Thanks a lot Man…it’s so Helpful…
Very helpful….thanks alot
dim nbre as integer =0
Private Sub dgv_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvEcheancier.CurrentCellDirtyStateChanged
nbre+= 1
With dgv
Select Case .CurrentCell.ColumnIndex
Case indexVoulu
Dim col As DataGridViewComboBoxColumn = .Columns(indexVoulu)
If TypeOf (col) Is DataGridViewComboBoxColumn Then
dgv.CommitEdit(DataGridViewDataErrorContexts.Commit)
If nbre = 2 Then
MessageBox.Show(.CurrentCell.Value)
nbre = 0
End If
End If
End Select
End With
hi i have used u r code but not run my code ..pls help me
I have used code…pls check it and wats pblm tell me…and me combobox name “ec”..
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
// Check box column
ComboBox comboBox = e.Control as ComboBox;
//comboBox.SelectedIndexChanged -= new EventHandler(comboBox_SelectedIndexChanged);
comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
}
}
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = ((ComboBox)sender).SelectedIndex;
string selecteditem = ((ComboBox)sender).SelectedItem.ToString();
SqlDataAdapter adp = new SqlDataAdapter(“select edd from cdl where id='”+selecteditem+”‘”, con);
DataSet ds = new DataSet();
adp.Fill(ds);
ec.DataSource = ds.Tables[0];
ec.DisplayMember = ds.Tables[0].Columns[“edd”].ToString();
ec.ValueMember = ds.Tables[0].Columns[“edd”].ToString(); ;
Dude. You fucking rock.