4
Reply

Unable to set focus to datagridviewCell after editingControl close

try abc

try abc

Nov 4 2011 9:16 AM
6.7k
Hello All,

I am working on datagridview Control. What i want to do is when i click on any cell , it should shows custom combobox and when i click on that combo's dropdown area, it shows another popup dialog. User can select Values from the dialog and when click  on the OK it will close the dialog, set the combo value with selected one and also set current cell's value.. 
The whole thing is working fine ,except Focus.. 

after New value is selected in Combo & grid cell, if now i am click on another cell it will require 3 click to set focus to new cell. 

I think there may be a Focus issue, when value is chaned in combo and cell , i have set focus back to grid as well as cell. 
but it still not working.

I have used Datagridview's dataGridView1_EditingControlShowing event to show combo when user enter cell. 

Here is the code.. 

  private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
                    {
                              comboBox1.Location= dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex,dataGridView1.CurrentCell.RowIndex,false).Location;
                              comboBox1.Size =new Size(e.Control.Size.Width+2,e.Control.Size.Height+10);
                              comboBox1.Value= e.Control.Text;
                              comboBox1.Visible = true;
                              comboBox1.selectedValueChanged += new SelectedValueChangeEventHandler(comboBox1_selectedValueChanged );
                    }

 void comboBox1_ selectedValueChanged(MyCombo. SelectedValueChangeEventArgs te)
                    {
                              dataGridView1.CurrentCell.Value = te.selectedValue;
                            //  dataGridView1.Select();
                              this.ActiveControl = dataGridView1;
                              dataGridView1.CurrentCell.Selected = true;
                              //dataGridView1.EditingControl.Focus();
                    }

I have try to set focus using ActiveControl, select() & Focus() , but none of them Worked.








Answers (4)