简体   繁体   中英

Selected Combobox value issue in datagridview

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.

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");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM