更改datagridview选择的行字体

时间:2018-04-01 21:13:02

标签: c#

我的datagridview属性中是否有任何选项可以更改所选行的字体? 或者我应该手动完成?

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

2 个答案:

答案 0 :(得分:1)

您可以使用DefaultCellStyle属性,例如

dataGridView.Rows[e.RowIndex].DefaultCellStyle.Font = 
           new Font(DataGridView.DefaultFont, FontStyle.Bold);

答案 1 :(得分:-2)

简单解决您的问题。

dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Blue;
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Red;

另一个也适合你的人。

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
{
  dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.Maroon;
  dataGridView1.CurrentRow.DefaultCellStyle.ForeColor = Color.White;
}