DataGridview - ComboBox

I have a DataGridView with 2 comboBoxColumns.  When I click on the combo in column 2 everything is good dgv_EditingControlShowing is called and rawComboChanged is called after it.  

But when i then click on column 3, rawComboChanged is called immediately, skipping the call to dgv_EditingControlShowing that should point the code towards currencyChanged method.  

public void dgv_EditingControlShowing(object sender, System.Windows.Forms.DataGridViewEditingControlShowingEventArgs e)
{
    DataGridView dataGridView = (DataGridView)sender;
    ComboBox cb = new ComboBox();
    if (dataGridView.CurrentCell.ColumnIndex == 2)
    {
        cb= e.Control as ComboBox;
        cb.SelectedIndexChanged += new System.EventHandler(this.rawComboChanged);
    }
    else if (dataGridView.CurrentCell.ColumnIndex == 3)
    {
         cb= e.Control as ComboBox;
         cb.SelectedIndexChanged += new System.EventHandler(this.currencyChanged);
    }
}

does anyone know how to fix it?
smaguireAsked:
Who is Participating?
 
JimBrandleyCommented:
I'm guessing he solved it himself, and that's fine with me.

Jim
0
 
JimBrandleyCommented:
Can you post the code from YourForm.Designer.cs where the event handlers are added?

Jim
0
 
smaguireAuthor Commented:
Sorry Jim for the delay,

here is both of my methods:

private void dgvChanConfig_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            cb = (ComboBox)e.Control;
            if (cb != null)
            {
                // first remove event handler to keep from attaching multiple:
                cb.SelectedIndexChanged -= new EventHandler(cb_SelectedIndexChanged);

                // now attach the event handler
                cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
            }
        }

        void cb_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedUnitName;
            selectedUnitName = cb.Text;
            SetComboBoxDataSource(comboboxMyColumn, "MyName", selectedUnitName);
            comboboxMyColumn.HeaderText = "Mill";
            //MessageBox.Show("Selected index changed");
            comboboxMyColumn.Selected = true;  
        }

only the first time it goes through the first method, and when i click the second time it goes directly to the second method (SelectedIndexChanged) Ignoring the first method.

thanks you
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
JimBrandleyCommented:
That's the event handler. Can you post the code where you assigned the event handler to the event?

Jim
0
 
smaguireAuthor Commented:
here it is:
this.dgvChanConfig.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dgvChanConfig_EditingControlShowing);

thanks
0
 
smaguireAuthor Commented:
hi Jim,
thanks for your reply, I went with a different solution but I will give you the points for trying to help me out.

thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.