winform中dataGridView隔行顯示不一樣的背景色,鼠標移動上顯示不一樣顏色,離開後變回原色orm
先設置奇數行顏色,這個有個自帶的屬性AlternatingRowsDefaultCellStyleblog
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue; //奇數行顏色
再在dataGridView上添加兩個事件,分別是CellMouseLeave和CellMouseMove事件
代碼以下:it
private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex > -1) { if (e.RowIndex % 2 == 0) dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White; else dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.AliceBlue; } } private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) { if (e.RowIndex > -1) { dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.PowderBlue; } }