Colonel_Custard
Colonel_Custard

Reputation: 1390

How do I check if a combobox in a datagridview equals a certain value?

I'm creating an ordering system for booking customers in, and I want a combobox in a datagridview to be able to mark a certain job as complete, and change the colour of that row to green when yes is selected, I'd like some help with this please as I have no idea how to do it, I have looked all over the internet and found nothing.

this is a screenshot of the database, any and all help would be greatly appreciated.

I'm using WinForms

thanks in advanceenter image description here

Upvotes: 0

Views: 2174

Answers (2)

gzaxx
gzaxx

Reputation: 17590

You have to use CellValueChanged event.

private void GridCellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    //just to be safe
    if (e.RowIndex < 0 || e.ColumnIndex < 0)
    {
        return;
    }

    var value = dataGridView1[e.ColumnIndex, e.RowIndex].Value;

    if (value != null && value.ToString() == "Yes")  // is completed
    {
       dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Green;
    }
    else
    {
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White;
    }
}

hope it helps :)

Upvotes: 3

Ehsan
Ehsan

Reputation: 32671

You should use Datagridview value changed event. And in that event get the selected row and cell value. Based on that value you should set the selected row backcolor.

Upvotes: 0

Related Questions