Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the datagridview line text in bold when I pick a row?

Tags:

c#

winforms

How do I make the datagridview line text in bold when I pick a row?

like image 868
Gali Avatar asked Oct 08 '11 10:10

Gali


2 Answers

Handle the CellFormatting event of the DataGridView and apply a bold style to the font if the cell belongs to a selected row:

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);
    // edit: to change the background color:
    e.CellStyle.SelectionBackColor = Color.Coral;
  }
}
like image 166
Julien Poulin Avatar answered Oct 27 '22 00:10

Julien Poulin


After loading the contents in Datagrid, apply these event handlers to RowEnter and RowLeave.

private void dg_RowEnter(object sender, DataGridViewCellEventArgs e)
{
    System.Windows.Forms.DataGridViewCellStyle boldStyle = new System.Windows.Forms.DataGridViewCellStyle();
    boldStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold);
    dg.Rows[e.RowIndex].DefaultCellStyle = boldStyle;
}

private void dg_RowLeave(object sender, DataGridViewCellEventArgs e)
{
    System.Windows.Forms.DataGridViewCellStyle norStyle = new System.Windows.Forms.DataGridViewCellStyle();
    norStyle.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular);
    dg.Rows[e.RowIndex].DefaultCellStyle = norStyle;
}

Codes are not tested. But it should work fine.

Hope it helps.

like image 26
Sandy Avatar answered Oct 26 '22 23:10

Sandy