How to make datagridview row text bold when I select row?

How to make datagridview row text bold when I select row?

+7
source share
4 answers

Handle the CellFormatting DataGridView event and apply bold style to the font if the cell belongs to the 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; } } 
+19
source

After loading the contents into the 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 checked. But it should work fine.

Hope this helps.

+1
source

Try handling the SelectionChanged dataGridView event and set the cell style.

0
source

In the code below, the font under Bold will be highlighted for the selected line. "Total" is the last line check in my code

 protected void gvRow_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.Cells[rowIndex].Text == "Total") { e.Row.Font.Bold = true; } } } 
0
source

All Articles