How to make the datagridview line text in bold whe

2020-07-08 04:58发布

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

标签: c# winforms
4条回答
叼着烟拽天下
2楼-- · 2020-07-08 05:27

The below code will make the font under Bold style for the selected row. "Total" is the last row 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;
  }
 }
}
查看更多
戒情不戒烟
3楼-- · 2020-07-08 05:31

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;
  }
}
查看更多
趁早两清
4楼-- · 2020-07-08 05:33

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.

查看更多
狗以群分
5楼-- · 2020-07-08 05:33

Try to handle SelectionChanged event of dataGridView and set cell style.

查看更多
登录 后发表回答