0

I have to manually select the item from the dropdownlist that is in the datagridview column, but the issue is while selecting the item i need to click on the dropdownlist multiple times.

How to resolve this? Any help will be highly appreciated.

DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.HeaderText = "Data"; 
cmb.Name = "cmb"; 
dgv2.Columns.Add(cmb);
Tim Pohlmann
  • 4,140
  • 3
  • 32
  • 61
AnCh
  • 29
  • 7
  • Please, post the relevant parts of your code. – varocarbas Sep 10 '15 at 08:37
  • DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn(); cmb.HeaderText = "Data"; cmb.Name = "cmb"; dgv2.Columns.Add(cmb); – AnCh Sep 10 '15 at 08:44
  • Thanks (although next time you might prefer to update your question by your own). This code is fine, although does not provide too much information. You are complaining about having to click multiple times to select a given item in the combobox. Provide code an/or information helping to understand this situation (not the case with the provided code). – varocarbas Sep 10 '15 at 08:49
  • dataGridView1.ColumnCount = 1; dataGridView1.Columns[0].Name = "Address"; DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn(); cmb.HeaderText = "Data"; cmb.Name = "Data"; dataGridView1.Columns.Add(cmb); string[] row = new string[] { "Address 2"}; dataGridView1.Rows.Add(row); row = new string[] { "Address 3"}; dataGridView1.Rows.Add(row); for(int i=0;i – AnCh Sep 10 '15 at 08:55
  • Possible duplicate of http://stackoverflow.com/q/13005112/2400754 – Sujith H S Sep 11 '15 at 06:50
  • @Sujith : But on that post answer is not marked. – AnCh Sep 11 '15 at 06:57

2 Answers2

0

The below code must be tied into the CellClick event of the datagridview:

private void datagridview_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        bool validRow = (e.RowIndex != -1); //Make sure the clicked row isn't the header.
        var datagridview = sender as DataGridView;

        // Check to make sure the cell clicked is the cell containing the combobox 
        if(datagridview.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && validRow)
        {
            datagridview.BeginEdit(true);
            ((ComboBox)datagridview.EditingControl).DroppedDown = true;
        }
    }
  1. Try Setting EditMode property to EditOnEnter. I hope this helps!
Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48
Nikita Shrivastava
  • 2,978
  • 10
  • 20
0

Set EditMode property of the DataGridView to EditOnEnter: link

DataGridView.EditMode - Gets or sets a value indicating how to begin editing a cell.

EditOnEnter - Editing begins when the cell receives focus.

Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48