如何在DataGridView1_CellMouseEnter事件中更改单元格的样式?
如题,当鼠标悬停在datagridview控件的某单元格上时,如何更改单元格中的字体颜色,并添加下划线,注意不是鼠标单击,而是指鼠标移到单元格上面时,就进行更改,
[解决办法]
- C# code
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;//鼠标移上变红色 dataGridView1.Rows[e.RowIndex].DefaultCellStyle.Font = new Font(this.Font.Name, this.Font.Size, FontStyle.Underline);//这个随便写自己试试看 } } private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Black;//鼠标离开黑色 } }
[解决办法]
- C# code
dataGridView1.SelectedRows[0].DefaultCellStyle.Font = new Font("宋体", 9, FontStyle.Strikeout);dataGridView1.SelectedRows[0].DefaultCellStyle.ForeColor = Color.Blue;dataGridView1.SelectedRows[0].DefaultCellStyle.BackColor = Color.Red;