2009-12-01 4 views
3

그림 형태, 윈폼, 그리고 닷넷 3.5된 DataGridView CellFormatting 이벤트 방지 나는 C#을 사용하고

내 양식을 정의 DataGridView있다 (as seen here, 내 cellformatting 이벤트 기간 동안 깜박 방지하기 위해 이중 버퍼링). 데이터베이스 검색을 수행 할 때 결과 데이터 집합을 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을 처리 할 때, 전체 폼의 Paint 이벤트가 꺼 보인다 것을 제외하고 잘 작동합니다. 커서가 텍스트 상자에 깜박이지, 폼의 menustrip은 다음과 같습니다

Menu bar picture http://img213.imageshack.us/img213/2430/menubar.jpg

상위가 검색하기 전에이며, 바닥 후. menuitar가있는 곳으로 마우스를 가져갈 때까지 menubar가 다시 그려지지 않을 것이며, 마우스를 menubar 밖으로 옮길 때 강조 표시되는 마지막 항목이 그대로 유지됩니다. 폼을 옮겨서 다시 칠하는 것처럼 보이지만 문제가 남아 있습니다.

datagridview 코드에서 resultsGridView.CellFormatting 행을 주석 처리하면 문제가 완전히 해결됩니다.

세포를 잘못 그린 것입니까? 아니면 처리해야 할 다른 것이 있습니까?

답변

1

아마도이 이벤트에서 예외가 발생했을 수 있습니다. 어떻게 처리가 정의되어 있는지 모르겠지만 try catch로 코드를 둘러 보는 것이 첫 번째 단계입니다.

try 
{ 
    int rowIndex = e.RowIndex; 
    .... 
} 
catch(Exception ex) 
{ 
    System.Diagnostics.Trace.Error(ex.message); 
} 

두 번째보기에서 나는 therow.Cells["Sealed"]이 작동하지 않을 것이라고 생각합니다. therow.Cells["dataGridViewTextBoxColumn2"]과 같은 것을 시도해보십시오. 셀은 이 아니라 데이터 프로퍼티 이름이 아닙니다.

+0

"봉인 된"부분은 데이터 속성이 아니라 내 데이터베이스의 bool 상태입니다. 내가 작업하고있는 프로그램은 이름에 대한 검색을 수행하고 부서의 사례 파일이 봉인되었는지 여부를 표시 할 수 있습니다. –

+0

catch 할 예외가 없습니다. 포맷팅 이벤트가 시작될 때'Systems.Diagnotics.Trace.WriteLine ("start")'을 추가했는데 셀이 디스플레이되고 아무 것도 진행되지 않아도 LOT가 실행 중입니다. –

+0

그러나 Dgv Column 객체의 이름을 "Sealed"합니까? –