0

hello all

I fill datagridview Row[0].Cells[0] with the following command

dgv1.Rows[0].Cells[(0].Value="-1";

//-------------------
how to give color so that the content "-1" in datagridview has red in color (forecolor)

thank you

denny

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by dennysimon
0

You can use the CellFormatting event to do this.
The following code snippet colours cells in the third column if they have a value of "-1".

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            var cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            if (e.ColumnIndex == 2 && ((string)cell.Value) == "-1")
            {
                e.CellStyle.ForeColor =  Color.Red;
            }
        }

You must check the column index to ensure the test is applied to the right data value.

Edited by nick.crane: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.