469,588 Members | 2,809 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,588 developers. It's quick & easy.

KeyDown Event On DGV Combobox Editable Column

maheshwag
I have a databound combobox column on DGV I am trying to fire a keys on keydown eventhandller on DGV as below:

Expand|Select|Wrap|Line Numbers
  1. private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             string connstr = "server=.;initial catalog=maa;uid=mah;pwd=mah";
  4.             SqlConnection con = new SqlConnection(connstr);
  5.             con.Open();
  6.             string sql = "select name from dummy";
  7.             SqlDataAdapter dap = new SqlDataAdapter(sql, con);
  8.             DataSet ds = new DataSet();
  9.             dap.Fill(ds);
  10.             DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
  11.             cmb.DataSource = ds.Tables[0];
  12.             cmb.DisplayMember = "name";
  13.             cmb.ValueMember = "name";
  14.  
  15.             dataGridView1.Columns.Add(cmb);
  16.             dataGridView1.Columns.Add("Srno.", "Srno.");
  17.             dataGridView1.Columns.Add("Name", "Name");
  18.  
  19.         }
  20.  
  21.  
  22.         private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
  23.         {
  24.             if (e.Control && e.KeyCode == Keys.C) 
  25.             {
  26.                 e.Handled = true;
  27.                 ss s = new ss();
  28.                 s.Show();
  29.  
  30.             }
  31.         }
  32.  
My problem is on editable datagridviewcomboboxcolumns the above keydown event is not fire. It is fire when I focus the another cell or move to another cell. If I wants to fire the above keydown event on editable combobox mode than how to do?.
Apr 11 '11 #1

✓ answered by GaryTexmo

It looks like you do it through the EditingControlShowing event on the DataGridView object itself. This event's EventArgs parameter has the control that is showing, so you can cast it to a ComboBox and assign an EventHandler to whatever event you want.

Since this event fires every time the editing control shows, you'll want to remove the event then add it so you don't get multiple event handlers. This is outlined on this Microsoft Connect page:

http://connect.microsoft.com/VisualS...ress-event-bug

Here's an example I quickly put together...

Expand|Select|Wrap|Line Numbers
  1.         public Form1()
  2.         {
  3.             InitializeComponent();
  4.  
  5.             DataGridView dgv = new DataGridView();
  6.             dgv.Columns.Add(new DataGridViewComboBoxColumn());
  7.             dgv.Columns.Add(new DataGridViewTextBoxColumn());
  8.             dgv.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dgv_EditingControlShowing);
  9.             dgv.Size = this.Size;
  10.  
  11.             this.Controls.Add(dgv);
  12.         }
  13.  
  14.         void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  15.         {
  16.             ComboBox cb = e.Control as ComboBox;
  17.             if (cb != null)
  18.             {
  19.                 cb.KeyPress -= new KeyPressEventHandler(cb_KeyPress);
  20.                 cb.KeyPress += new KeyPressEventHandler(cb_KeyPress);
  21.             }
  22.         }
  23.  
  24.         void cb_KeyPress(object sender, KeyPressEventArgs e)
  25.         {
  26.             Console.WriteLine(e.KeyChar.ToString());
  27.         }

3 5819
GaryTexmo
1,501 Expert 1GB
If I'm reading you right, you want the key down event to happen on the combo box, not on the DataGridView?

From the code you posted (which isn't very much), it seems you're attaching the event to the DataGridView object, can you instead put it on the combo box?
Apr 11 '11 #2
Garytexmo

Do you mean on datagridviewcomboboxcolumn key event?. Is it possible?. how?
Apr 11 '11 #3
GaryTexmo
1,501 Expert 1GB
It looks like you do it through the EditingControlShowing event on the DataGridView object itself. This event's EventArgs parameter has the control that is showing, so you can cast it to a ComboBox and assign an EventHandler to whatever event you want.

Since this event fires every time the editing control shows, you'll want to remove the event then add it so you don't get multiple event handlers. This is outlined on this Microsoft Connect page:

http://connect.microsoft.com/VisualS...ress-event-bug

Here's an example I quickly put together...

Expand|Select|Wrap|Line Numbers
  1.         public Form1()
  2.         {
  3.             InitializeComponent();
  4.  
  5.             DataGridView dgv = new DataGridView();
  6.             dgv.Columns.Add(new DataGridViewComboBoxColumn());
  7.             dgv.Columns.Add(new DataGridViewTextBoxColumn());
  8.             dgv.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dgv_EditingControlShowing);
  9.             dgv.Size = this.Size;
  10.  
  11.             this.Controls.Add(dgv);
  12.         }
  13.  
  14.         void dgv_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  15.         {
  16.             ComboBox cb = e.Control as ComboBox;
  17.             if (cb != null)
  18.             {
  19.                 cb.KeyPress -= new KeyPressEventHandler(cb_KeyPress);
  20.                 cb.KeyPress += new KeyPressEventHandler(cb_KeyPress);
  21.             }
  22.         }
  23.  
  24.         void cb_KeyPress(object sender, KeyPressEventArgs e)
  25.         {
  26.             Console.WriteLine(e.KeyChar.ToString());
  27.         }
Apr 11 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Mike M | last post: by
3 posts views Thread by bardo | last post: by
reply views Thread by Kartic | last post: by
1 post views Thread by fripper | last post: by
5 posts views Thread by ApexData | last post: by
3 posts views Thread by win | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.