how to set SelectedIndex in DataGridViewComboBoxColumn?

后端 未结 7 848
慢半拍i
慢半拍i 2021-01-18 04:00

i am using a datagridview in that i am using a datagridviewcomboboxcolumn, comboboxcolumn is displaying text but the problem is i want to select the first item of comboboxco

7条回答
  •  逝去的感伤
    2021-01-18 04:41

    If I had known about doing it in this event, it would have saved me days of digging and
    trial and errors trying to get it to set to the correct index inside the CellEnter event.

    Setting the index of the DataGridViewComboBox is the solution I have been looking for.....THANKS!!!

    In reviewing all the issues other coders have been experiencing with trying to set
    the index inside of a DataGridViewComboBoxCell and also after looking over your code,
    all that anyone really needs is:
    1. Establish the event method to be used for the "EditingControlShowing" event.
    2. Define the method whereby it will:
        a. Cast the event control to a ComboBox.
        b. set the "SelectedIndex" to the value you want.
            In this example I simply set it to "0", but you'd probably want to apply so real life logic here.

    Here's the code I used:

    private void InitEvents()
    {
    
        dgv4.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler( dgv4EditingControlShowing );
    
    }
    
    
    private void dgv4EditingControlShowing( object sender, DataGridViewEditingControlShowingEventArgs e )
    {
       ComboBox ocmb = e.Control as ComboBox;
       if ( ocmb != null )
       {
          ocmb.SelectedIndex = 0;
       }
    }
    

提交回复
热议问题