怎么实现dataGridView选择中行获得焦点的单元格背景的设置,失去焦点后,背景颜色为默认背景

解决方案 »

  1.   

    private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
            {
                this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.AliceBlue;
            }
      

  2.   


      private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                
                if (e.RowIndex == -1)
                {
                    //绘制渐变列头
                    //bool mouseOver = e.CellBounds.Contains(dataGridView1.PointToClient(Cursor.Position));
                    //LinearGradientBrush brush = new LinearGradientBrush(
                    //    e.CellBounds,
                    //    mouseOver ? Color.PeachPuff : Color.LightGray,
                    //    Color.DarkOrange,
                    //    LinearGradientMode.Vertical);                //using (brush)
                    //{
                    //    e.Graphics.FillRectangle(brush, e.CellBounds);
                    //    Rectangle border = e.CellBounds;
                    //    border.Width -= 1;
                    //    e.Graphics.DrawRectangle(Pens.Gray, border);
                    //}                // 绘制背景色
                    e.Graphics.DrawImage(global::WindowsApplication6.Properties.Resources.主页0, e.CellBounds.X, e.CellBounds.Y);                e.PaintContent(e.CellBounds);
                    e.Handled = true;
                }        }
      

  3.   

    private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
      {
      this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.AliceBlue;
      } 
    当你选择另外一行的时候,上一行的单元格背景没有换成默认背景  怎么处理呢?我就是想在选择的那一行的时候,光标在的单元格换成特殊背景,其他行的单元格为默认背景就可以了
      

  4.   

    private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
      {
      this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.AliceBlue;
      }
      

  5.   

    int index=dgv.SelectedRows[0].Index;当鼠标移开后,
    this.dataGridView1.Rows[index].Cells[e.ColumnIndex].Style.BackColor = Color.White