Kelsall
Kelsall

Reputation: 57

Apply bold font to two consecutive rows in a datagridview

I have two consecutive rows in a datagridview, both of which need to be bold. I have changed the text colour for the lower row to red as follows. Although I can't seem to find a similar bold function.

cashBookRowsMarchTotals.Rows[1].DefaultCellStyle.ForeColor = Color.Red;

Upvotes: 0

Views: 3496

Answers (3)

Monzur
Monzur

Reputation: 1435

Use Poperty of DataGridView

RowsDefaultCellStyle Click on Ellipse [...] and Set the Font Properties

Upvotes: 0

Pabinator
Pabinator

Reputation: 1641

I saw your answer and I feel like let you know about the CellFormatting Event of the dataGridView. It is actually built to format the dataGridView based from the row index or column values.

This code example changes the font based on the value of a column but you can easily change it to filter by the row index instead of the column value.

    private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        // Change the color of the row if the value on column1 is > 0
        if (e.ColumnIndex == 1 && Convert.ToDecimal(e.Value) > 0)  // Column1
        {
            this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;  // Set font color red
            this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.Font = new System.Drawing.Font(this.Font, FontStyle.Bold);   // Set Bold   
        }
        if (e.ColumnIndex == (int)transactionsDGV.AmountDue && Convert.ToDecimal(e.Value) <= 0)  // Column Amount Due
        {
            this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;  // Set font color black  
        }
    }

Upvotes: 0

Kelsall
Kelsall

Reputation: 57

I made the two rows bold using the following code, but was wondering if it is possible to apply bold (or other styles) to multiple rows/columns at a time?

cashBookRowsMarchTotals.Rows[0].DefaultCellStyle.Font = new Font(cashBookRowsMarchTotals.Font, FontStyle.Bold); cashBookRowsMarchTotals.Rows[1].DefaultCellStyle.Font = new Font(cashBookRowsMarchTotals.Font, FontStyle.Bold);

Upvotes: 1

Related Questions