'How to manually select the DataGridviewComboBoxCell Value in CellContentClick Event?

Hi Programmers, Actually I have a DataGridViewComboBoxCell in DataGridvIew and I need to change the DataGridViewComboBox value if the condition is true when the CellContentClick event is fired. My Code Goes Like this:

    private void gridviewholiday_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
    {
        int row = e.RowIndex;
        int colo = e.ColumnIndex;


        /*=============== To Show The Details  =====================*/

        if (e.ColumnIndex == 4)
        {
            if (Convert.ToBoolean(gridviewholiday.Rows[e.RowIndex].Cells[0].Value))
            {
                if (Type == "CUS")
                {
                    Type = test.colType;
                    if (Type == "NO")
                    {


                        ComboBox combo = (ComboBox)sender;
                        combo.SelectedIndex = 0;

                    }
                }
    }

But it gives error while casting DataGridView to Combobox.

Please help me out.



Solution 1:[1]

Try This

Private Sub dgvMain_CellMouseEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgvMain.CellMouseEnter
        Dim dgv As DataGridView
        dgv = DirectCast(sender, DataGridView)
        If dgv IsNot Nothing Then
            Dim cmb As DataGridViewComboBoxCell 
            cmb = DirectCast(dgv.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewComboBoxCell)
            If cmb IsNot Nothing Then
                cmb.Selected = True
            End If
        End If
    End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rob Jones