Click here to Skip to main content
15,887,027 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.
Posted
Updated 14-Feb-11 1:01am
v5

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.

[Update]
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.
[/Update]

[Update2]
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. :)
[/Update2]

[Update3]
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:
C#
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()
        {
            InitializeComponent();
        }

        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.Columns.Add("typename");
            typedt.Columns.Add("typedescription");
            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");
            typedt.AcceptChanges();

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

            dt.AcceptChanges();

            // 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.RemoveAt(2);
            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();
                    this.dataGridView1.EndEdit();
                }
            }
        }
    }
}


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. :)
[/Update3]
 
Share this answer
 
v5
Comments
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 :)

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