2014-11-03 7 views
0

내 화면 중 하나에서 DataGridViewCheckBoxCells을 많이 사용하는 애플리케이션이 있습니다. 간단히 말해, 나는 실행 시스템이 포함 된 테이블을 VirtualMode에 설정하고 유효성 검사 등을 수행합니다. 경우에 따라 다른 상자가 처음 확인되지 않은 상자를 확인하는 등의 의미는 없습니다. 사용자가 할 수있게했지만 셀의 ErrorText 속성을 설정하고 계속할 수있는 확인 기능이 있습니다.현재 DataGridViewCheckBoxCell에 오류를 표시하려면 어떻게합니까?

어쩌구. 이미 코드가 ErrorText 또는 그와 비슷한 것을 삭제하지 않았 음을 확인했습니다. 문제는 DataGridViewCheckBoxCells입니다. 오류 아이콘/글리프가 현재 셀에 페인트 칠하지 마십시오. 나는 왜 그들이 이런 방식으로 설계되었는지 이유를 알 수 없다. 실제로 참조 소스 (DataGridViewCheckBoxCell.cs는) 저를 백업합니다

DataGridViewCheckBoxCell.cs에서

:

protected override Rectangle GetErrorIconBounds(Graphics graphics, DataGridViewCellStyle cellStyle, int rowIndex) 
{ 
    // some checks ... 

    Point ptCurrentCell = this.DataGridView.CurrentCellAddress; 
    if (ptCurrentCell.X == this.ColumnIndex && 
     ptCurrentCell.Y == rowIndex && this.DataGridView.IsCurrentCellInEditMode) 
    { 
     // PaintPrivate does not paint the error icon if this is the current cell. 
     // So don't set the ErrorIconBounds either. 
     return Rectangle.Empty; 
    } 

    // behavior ... 
} 

DataGridViewCheckBoxCell.PaintPrivate 살펴 복용 :

private Rectangle PaintPrivate(Graphics g, 
    Rectangle clipBounds, 
    Rectangle cellBounds, 
    int rowIndex, 
    DataGridViewElementStates elementState, 
    object formattedValue, 
    string errorText, 
    DataGridViewCellStyle cellStyle, 
    DataGridViewAdvancedBorderStyle advancedBorderStyle, 
    DataGridViewPaintParts paintParts, 
    bool computeContentBounds, 
    bool computeErrorIconBounds, 
    bool paint) 
{ 
    // blah blah 

    // here it determines "If I am the current cell DO NOT draw the error icon!" 
    Point ptCurrentCell = this.DataGridView.CurrentCellAddress; 
    if (ptCurrentCell.X == this.ColumnIndex && 
     ptCurrentCell.Y == rowIndex && this.DataGridView.IsCurrentCellInEditMode) 
    { 
     drawErrorText = false; 
    } 

    // sure enough, drawErrorText must be true here for the error icon to be drawn 
    if (paint && DataGridViewCell.PaintErrorIcon(paintParts) && drawErrorText && this.DataGridView.ShowCellErrors) 
    { 
     PaintErrorIcon(g, cellStyle, rowIndex, cellBounds, errorBounds, errorText); 
    } 
} 

가 어떻게이 문제를 해결받을 수 있습니까? 나는 작은 서브에 DataGridViewCheckBoxCell를 하위 클래스로 만들려고했다. 너무 많은 내부 함수와 상수가 있습니다. 이 테이블을 사용하는 Form에서 해킹하려고했는데 어쨌든 (reasons) 가로 채고있는 DirtyStateChanged 다음에 DataGridViewCheckBoxCell 인 경우 현재 셀을 선택 취소했습니다. 그러나 이것은 작동하지 않습니다. 사용자가 셀을 선택하면 오류 아이콘이 사라집니다. 셀 값을 편집하는 과정에서 selection이 첫 번째 단계이기 때문에 DataGridViewCheckBoxCell을 차단하고 차단할 수 없습니다 (SelectionChanged).

어떻게해야합니까?

+1

[이 포스트] (http://stackoverflow.com/questions/24562966/reposition-the-error-icon-of-a-datagridview/24600915# : 당신은 당신의 특정한 경우에 적용해야 할 수도 24600915) 해결 방법을 설명합니다 : ErrorIcon 대신 Backcolor를 사용하여 오류를 나타냅니다 .. – TaW

+0

그럴 수 있습니다. 감사합니다. 불행히도, 나는 이미 셀의 컨텍스트 또는 그 의미를 시각적으로 이해하기 위해'BackColor'를 사용하고 있습니다. 하지만 아마도 두 색상 사이를 앞뒤로 "깜박"으로써 이들을 서로 맞물리게 할 수 있습니다. – Gutblender

답변

0

다음 코드는 오류 텍스트가있는 모든 셀에 "아름다운"오류 아이콘을 그립니다.

static Icon m_errorIcon; 

public static Icon ErrorIcon 
{ 
    get 
    { 
     if (m_errorIcon == null) 
     { 
      ErrorProvider errorProvider = new ErrorProvider(); 
      m_errorIcon = errorProvider.Icon; 
      errorProvider.Dispose(); 
     } 
     return m_errorIcon; 
    } 
} 

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) 
{ 
    try 
    { 
     if ((e.PaintParts & DataGridViewPaintParts.ErrorIcon) != 0 && !string.IsNullOrEmpty(e.ErrorText)) 
     { 
      Icon icon = ErrorIcon; 
      int pixelMargin = 2; 

      Rectangle cellBounds = new Rectangle(e.CellBounds.X + pixelMargin, e.CellBounds.Y, e.CellBounds.Width - 2 * pixelMargin, e.CellBounds.Height); 
      Rectangle firstIconRectangle = new Rectangle(cellBounds.Left, cellBounds.Top + Math.Max((cellBounds.Height - icon.Width)/2, 0), Math.Min(icon.Width, cellBounds.Width), Math.Min(icon.Width, cellBounds.Height)); 

      e.Paint(e.ClipBounds, e.PaintParts & ~DataGridViewPaintParts.ErrorIcon); 
      e.Graphics.DrawIcon(icon, firstIconRectangle); 
      e.Handled = true; 
     } 
    } 
    catch (Exception exception) 
    { 
    } 
}