How do you highlight the row and/or column labels of a datagridview on mouseover of any cell (in c#)?

前端 未结 3 1533
太阳男子
太阳男子 2021-01-07 05:36

With a DataGridView control on a Windows form, when you move the mouse over a row label (or column label) it\'s (the label cell) background changes to a shade of blue (or ot

3条回答
  •  情深已故
    2021-01-07 06:03

    You can hook into the DataGridView's CellMouseEnter and CellMouseLeave events and then change the backcolor accordingly. Something like this:

        private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0) //column header / row headers
            {
                return;
            } 
    
            this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.LightBlue;
        }
    
        private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0) //column header / row headers
            {
                return;
            }
    
            this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.White;
        }
    

提交回复
热议问题