Selected Combobox value issue in datagridview

1k views Asked by At

I have a datagridview table with combobox.

Now I want to check whether the combobox value is selected when a row is checked.

if (combobox.Selected.ToString() != null && selectedRowCount !=0)
{
    MessageBox.Show("Combobox value is selected");

}   
else
{                    
    MessageBox.Show("Please select combox value!");
}

But this doesn't seem to work. Please advise.

1

There are 1 answers

0
GER On BEST ANSWER

Here is an example of checking the value in the column of the DataGridViewComboBoxColumn.

In the CellClick event we check the columns value instead of the combo's value.

class Program
{
    [STAThread()]
    static void Main(string[] args)
    {
        Form f = new Form();
        DataGridView dgv = new DataGridView();
        DataGridViewComboBoxColumn dgvCombo = new DataGridViewComboBoxColumn();

        //Setup events
        dgv.CellClick += dgv_CellClick;


        //Add items to combo
        dgvCombo.Items.Add("Item1");
        dgvCombo.Items.Add("Item2");

        //Add combo to grid
        dgv.Columns.Insert(0,dgvCombo);

        //Add grid to form
        f.Controls.Add(dgv);
        dgv.Dock = DockStyle.Fill;
        f.ShowDialog(null);
    }

    static void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridView grid = (DataGridView) sender;

        //Check index 0 because the ComboBox is in that column
        if (grid.SelectedCells[0].OwningRow.Cells[0].Value != null)
        {
            MessageBox.Show("A value is selected");
        }
        else
        {
            MessageBox.Show("No Value is selected");
        }
    }
}