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

Recommended Answers

All 2 Replies

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.

hi nick crane

thank you

denny

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.