Click here to Skip to main content
14,739,737 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

I have a dataGridView in which there exist a combobox column.
My problem is when I change the selected item in a cell of this column , it must change another cell content in the dataGridView.

I tried to use CellValueChanged event but the other cell content is changing after leaving the cell not directly after changing the selected item.
I also tried CurrentCellDirtyStateChanged event and it gives me the same problem.
I have tried also the link[^] given by Mr.Nuri but I don't know why it wasn't running correctly, it act when I leave the combobox cell and returning again.

This is the code I have tried :

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    //This is private variable ComBoBox.
    if (combo != null)
        combo = (ComboBox)e.Control;
        combo.SelectedIndexChanged +=new EventHandler(combo_SelectedIndexChanged);

private void combo_SelectedIndexChanged(object sender, EventArgs e)
    // I put my code here for editing the other cell

Please can anyone helps me,
Thank you.
Updated 14-Feb-11 2:01am

1 solution

Yes, CellValueChanged event typically occurs when focus leaves the cell. You can check this in documentation here[^].
I believe for your case you can use DataGridView.CurrentCellDirtyStateChanged[^] event.

You can check out DataGridViewComboBoxEditingControl[^] class.
I've never tried it but the documentation contains an interesting (and probably useful for you) example - adding an event handler for ComboBox.SelectedIndexChanged event for DataGridViewComboBoxColumn.

After a little research on the topic it turns out that DataGridViewComboBoxEditingControl is exactly what you need. I've already give you the link to its documentation with example, but this time I found some useful links with complete examples which do exactly what you want:
- click1[^]
- click2[^]

I hope this helps. :)

Drear Michael,

I just created a working example with the code from this link[^]. I've created a new Windows Forms project and added an empty DataGridView (the form designer will add it as dataGridView1) to the form. The rest is here:
namespace WindowsFormsApplication1
    public partial class Form1 : Form
        ComboBox cbm;
        DataGridViewCell currentCell;

        // This DataTable will become a data source for the DataGridView
        DataTable dt = new DataTable();

        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            // Create and fill a DataTable as a source of our combobox column
            DataTable typedt = new DataTable();
            typedt.Columns.Add("typeid", typeof(int));
            typedt.Rows.Add(0, "Typed 0", "description0");
            typedt.Rows.Add(1, "Typed 1", "description1");
            typedt.Rows.Add(2, "Typed 2", "description2");
            typedt.Rows.Add(3, "Typed 3", "description3");
            typedt.Rows.Add(4, "Typed 4", "description4");

            // Fill the GridView source table
            dt.Columns.Add("type", typeof(int));
            for (int i = 0; i < 50; i++)
                dt.Rows.Add(i.ToString("000"), "name" + i, i % 4);


            // Set the data source for the grid
            dataGridView1.DataSource = dt;

            // Create new combobox column and set it's data source
            DataGridViewComboBoxColumn cbc = new DataGridViewComboBoxColumn();
            cbc.DataPropertyName = "type";
            cbc.DisplayMember = "typename";
            cbc.ValueMember = "typeid";
            cbc.DataSource = typedt;
            cbc.HeaderText = "Type";

            // Replace the third column of the grid view with the combobox column
            dataGridView1.Columns.Insert(2, cbc);

            // We will handle these events of the DataGridView
            dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
            dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);

        void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            if (cbm != null)
                // Here we will remove the subscription for selected index changed
                cbm.SelectedIndexChanged -= new EventHandler(cbm_SelectedIndexChanged);

        void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            // Here try to add subscription for selected index changed event
            if (e.Control is ComboBox)
                cbm = (ComboBox)e.Control;
                if (cbm != null)
                    cbm.SelectedIndexChanged += new EventHandler(cbm_SelectedIndexChanged);
                currentCell = this.dataGridView1.CurrentCell;

        void cbm_SelectedIndexChanged(object sender, EventArgs e)
            // Invoke method if the selection changed event occurs
            BeginInvoke(new MethodInvoker(EndEdit));

        void EndEdit()
            // Change the content of appropriate cell when selected index changes
            if (cbm != null)
                DataRowView drv = cbm.SelectedItem as DataRowView;
                if (drv != null)
                    this.dataGridView1[currentCell.ColumnIndex + 1, currentCell.RowIndex].Value = drv[2].ToString();

It is working fine for me and the content of the other cell is changed without leaving the ComboBox. Give it a try and if it doesn't work maybe you should post your actual code to see where is the problem. But as I said it is working fine for me and it should work for you too. :)
Michael Waguih 14-Feb-11 4:33am
Thanks I have tried it but it gives me the same as CellValueChanged event
Member 4596757 12-Dec-13 4:47am
Thnx Buddy
Nuri Ismail 14-Feb-11 5:05am
Please check out my update, it might help. :)
Michael Waguih 14-Feb-11 6:14am
I have tried it but I don't know why it wasn't running correctlyit act when I leave the combobox cell and returning again
Michael Waguih 14-Feb-11 6:54am
Thnx Mr.Nuri for your help :)
Nuri Ismail 14-Feb-11 8:20am
Micheal, I just created an example and it works fine for me. Check out "Update3". :)
Michael Waguih 14-Feb-11 9:05am
I test it it is really wonderful , I will try to simulate it in my code ,
Thanks a lot Nuri :)
Nuri Ismail 14-Feb-11 9:22am
You're welcome Michael! :)
Michael Waguih 16-Feb-11 4:13am
Sorry Nuri but I have an existing comboBox column how can I do it ? I don't want to create a new column
Michael Waguih 16-Feb-11 4:23am
Thanks I fixed it :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900