How to leave the selected line font in bold in DataGridView?

1

In the DataGridView of Windows Forms it is possible to change the font color and background color of the selected row easily using the

DataGridView.DefaultCellStyle.SelectionBackColor
DataGridView.DefaultCellStyle.SelectionForeColor

But what about to leave the selected line with the font in bold? I already tried some things like using the SelectionChanged event as follows:

private void Grid_SelectionChanged(object sender, EventArgs e)
{
    var dataGridView = Grid;

    if (dataGridView.SelectedRows.Count > 0)
    {
        var selectedRow = dataGridView.SelectedRows[0];
        selectedRow.DefaultCellStyle.Font = new Font(dataGridView.Font, FontStyle.Bold);
    }            
}

It turns out that this always leaves all the lines with the font in bold, so it's not quite there.

What's the right way to do this?

    
asked by anonymous 12.07.2016 / 19:13

2 answers

1

Try something like this:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  var dataGridView = sender as DataGridView;
  if (dataGridView.Rows[e.RowIndex].Selected)
  {
    e.CellStyle.Font = new Font(e.CellStyle.Font, FontStyle.Bold);
    e.CellStyle.SelectionBackColor = Color.Coral;
  }
}
    
12.07.2016 / 20:53
0

Good evening,

Have you tried using Grid properties?

Otherwise: Click on Grid then search the properties tab on the right side, the RowDefaultCellStyle or RowTemplate properties that might resolve your issue. So you do not have to leave it dynamically in your code. The object's own properties already do this for you.

    
13.07.2016 / 00:27