0

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.

james andy
  • 59
  • 1
  • 10
  • Show how you filling `combobox` with values. And notice that combobox haven't such a property as `.Selected`. – Fabio Jun 12 '15 at 17:47
  • Is question about `DataGridViewComboBoxColumn`? – Fabio Jun 12 '15 at 19:22
  • Yes I have DataGridViewComboBoxColumn combobox – james andy Jun 12 '15 at 20:03
  • There is a Syntax error. Inside your if statement the string is not quoted. It should be `MessageBox.Show("Combobox value is selected");` Is that the problem you are asking about? – GER Jun 16 '15 at 17:58
  • That is my typing mistake. But main thing is that I am not getting the condition to work for DatagridViewCombobox – james andy Jun 17 '15 at 17:01
  • What event are you working inside of? I think you have to look at the cell value instead of the combo box. I posted an answer that works off the cell click event that may help. – GER Jun 18 '15 at 16:12

1 Answers1

0

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");
        }
    }
}
GER
  • 1,870
  • 3
  • 23
  • 30