2016-02-05 68 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; 
    } 
} 

順便說一句,請確保您處理任何定製的刷子和筆。