Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Button in DataGridView with condition C#

$
0
0

Hello,

I have datagridView, i ADD new field 'Check_Out' with this code:

 DataGridViewButtonColumn column = new DataGridViewButtonColumn();
            dataGridView1.Columns.Add(column);
            column.FlatStyle = FlatStyle.System;
            column.Name = "Check-Out";
            column.HeaderText = "Check-Out";
            column.Text = "Check-Out";
            column.UseColumnTextForButtonValue = true;

Now, all the button in Column 'Check-Out' work...with the Red Row or the White Row...

my code:

int row = (int)this.dataGridView1.CurrentCell.OwningRow.Cells[0].Value;

            if (MessageBox.Show("Check-out?","Message de confirmation",
                              MessageBoxButtons.YesNo) == DialogResult.Yes)
            {    MessageBox.Show("Success!!");
}

But I want just the button ,when the color of the ROW is RED , works, (When the ROW has color WHITE, nothing is happening!the button doesn't work)
I try this code:

if (dataGridView1.SelectedRows[0].DefaultCellStyle.BackColor == Color.Red)
        {
            int row = (int)this.dataGridView1.CurrentCell.OwningRow.Cells[0].Value;

            if (MessageBox.Show("Check-out?","Message de confirmation",
                              MessageBoxButtons.YesNo) == DialogResult.Yes)
            {MessageBox.Show("Success!!");
}

I have ERROR: The index was of limits. It must not be negative and must be less than the size of the collection. in line:

if (dataGridView1.SelectedRows[0].DefaultCellStyle.BackColor == Color.Red)

ERROR:

How can I fix it?

Very Thanks,


Viewing all articles
Browse latest Browse all 2535

Trending Articles