简体   繁体   English

当我选择一行时,如何使datagridview行文本以粗体显示?

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

当我选择一行时,如何以粗体显示datagridview行文本?

Handle the CellFormatting event of the DataGridView and apply a bold style to the font if the cell belongs to a selected row: 处理DataGridViewCellFormatting事件,如果单元格属于选定的行,则将粗体样式应用于字体:

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;
  }
}

After loading the contents in Datagrid, apply these event handlers to RowEnter and RowLeave. 在Datagrid中加载内容后,将这些事件处理程序应用于RowEnter和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. 希望能帮助到你。

The below code will make the font under Bold style for the selected row. 下面的代码将使所选行的Bold样式下的字体。 "Total" is the last row check in my code “Total”是我的代码中的最后一行检查

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;
  }
 }
}

尝试处理dataGridView的SelectionChanged事件并设置cell样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM