DatagridView Cellformatting 이벤트 형태 그림을 방지합니다
-
11-09-2019 - |
문제
C#, Winforms 및 .NET 3.5를 사용하고 있습니다.
내 양식에는 관습이 있습니다 DataGridView
(내 세포 성형 이벤트 중 깜박임을 방지하기 위해 이중 청분, 여기에서 볼 수 있듯이). 데이터베이스 검색을 수행 할 때 결과 데이터 세트를 datagridview
.
나는 처리한다 CellFormatting
데이터에 따라 행을 특정 색상으로 페인트하는 이벤트.
내 datagridView 코드 :
resultsGridView.DataSource = results.DefaultViewManager.DataSet.Tables[0];
resultsGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.AliceBlue;
resultsGridView.BorderStyle = BorderStyle.Fixed3D;
resultsGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(resultsGridView_CellFormatting);
내 CellFormatting 코드 :
void resultsGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
int rowIndex = e.RowIndex;
DataGridViewRow therow = resultsGridView.Rows[rowIndex];
if ((bool)therow.Cells["Sealed"].Value == true)
{
therow.DefaultCellStyle.BackColor = Color.Pink;
}
if (therow.Cells["Database"].Value as string == "PNG")
{
therow.DefaultCellStyle.BackColor = Color.LightGreen;
}
}
CellFormatting을 처리 할 때 전체 형태의 페인트 이벤트가 꺼져있는 것 같습니다. 커서는 텍스트 상자에서 깜박임을 멈추고 양식의 menustrip은 다음과 같습니다.
상단은 검색 전입니다. 메뉴 바는 내가 menuitems가있는 곳을 마우스 할 때까지 다시 그리지 않을 것입니다. 그리고 마지막으로 강조 될 항목은 마우스를 메뉴 바에서 옮길 때 그렇게 유지됩니다. 양식을 이동하면 다시 칠한 것으로 보이지만 문제는 여전히 남아 있습니다.
댓글을 달았습니다 resultsGridView.CellFormatting
DatagridView 코드의 줄은 문제를 완전히 해결합니다.
세포를 잘못 그림을 그렸습니까, 아니면 다루어야 할 다른 것이 있습니까?
해결책
아마도이 이벤트 내에서 예외를 일으킬 것입니다. 처리가 어떻게 정의되는지 잘 모르겠지만, 코드를 둘러싼 시도 캐치와 함께 첫 단계가 될 것입니다.
try
{
int rowIndex = e.RowIndex;
....
}
catch(Exception ex)
{
System.Diagnostics.Trace.Error(ex.message);
}
두 번째로, 나는 생각하지 않습니다 therow.Cells["Sealed"]
작동합니다. 같은 것을 시도하십시오 therow.Cells["dataGridViewTextBoxColumn2"]
. 세포는 색인에 의해 색인됩니다 열 이름, 아니다 DatapRopertyName.