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

前端 未结 3 1519
太阳男子
太阳男子 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:06

    I know you already have the response for this, but I will share something different.

    This way, the whole row is painted. I just modified @BFree comments a little bit.

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

提交回复
热议问题