2012-11-23 46 views

回答

0

我已經通過處理CellFormatting事件更改了DataGridViews的顏色。我這樣做是爲了突出顯示錯誤的行,或突出顯示特定的列;

在我的表單init方法中,我有類似的東西;

dgvData.CellFormatting += 
      new DataGridViewCellFormattingEventHandler(dgvData_CellFormatting); 

並且負責格式化的方法如下;

 private void dgvData_CellFormatting(object sender, 
            DataGridViewCellFormattingEventArgs e) 
     { 
      bool inError = false; 

      // Highlight the row as red if we're in error displaying mode 
      if (e.RowIndex >= 0 && fileErrors != null && DisplayErrors) 
      { 
       // Add +1 to e.rowindex as errors are using a 1-based index 
       var dataErrors = (from err in fileErrors 
            where err.LineNumberInError == (e.RowIndex +1) 
            select err).FirstOrDefault(); 
       if (dataErrors != null) 
       { 
        e.CellStyle.BackColor = Color.Red; 
        inError = true; 
       } 
      } 

      // Set all the rows in a column to a colour, depending on it's mapping. 
      Color colourToSet = GetBackgroundColourForColumn(dgvData.Columns[e.ColumnIndex].Name); 
      if (colourToSet != null && !inError) 
       e.CellStyle.BackColor = colourToSet; 
     } 

爲了特定小區做到這一點,你可能還需要處理在控制MouseUp事件,然後使用數據網格視圖的HitTestInfo摸出命中竟是哪個小區。