2012-11-23 4 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를 사용해야 할 수도 있습니다.

관련 문제