2016-02-05 54 views
1

我得到的代码是将行的所有单元格着色,但是我需要着色某个/当前单元格。如何给DataGridView中的当前单元格着色

它是如何做到的?

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
     { 
      if (e.RowIndex > -1) 
      { 
       ThemeColorView tc = (dataGridView1.Rows[e.RowIndex].DataBoundItem as ThemeColorView); 

       if (tc != null) 
       { 
        Color c = Color.FromName(tc.ColorType.Trim()); 
        Brush b = new SolidBrush(c); 

        // I know something shoud be changed here ;) 
        e.Graphics.FillRectangle(b, e.CellBounds); 
        e.PaintContent(e.ClipBounds); 

        e.Handled = true; 
       } 
      } 
     } 

回答

2

尝试画细胞比较网格的CurrentCell:

if (e.RowIndex > -1 && dataGridView1.CurrentCell != null) { 
    if (e.ColumnIndex == dataGridView1.CurrentCell.ColumnIndex && 
     e.RowIndex == dataGridView1.CurrentCell.RowIndex) { 
     e.Graphics.FillRectangle(Brushes.Green, e.CellBounds); 
     e.PaintContent(e.CellBounds); 
     e.Handled = true; 
    } 
} 

顺便说一句,请确保您处理任何定制的刷子和笔。

相关问题