2012-06-15 4 views
1

개발중인 컨트롤에 대해 DataGridView 컨트롤을 상속하고 있습니다. 목표는 런타임에 변경할 수있는 객체 상태를 나타내는 각 행 색상을 만드는 것입니다. 내 개체가 Observable 디자인 패턴을 구현합니다. 그래서 내 자신의 DataGridViewRow 클래스를 개발하고 Observer 패턴을 구현하고 행을 객체를 관찰하기로 결정했습니다. 나는 인 selectionchanged 이벤트를 선택한 행에 내 UpdateColors 메서드를 호출런타임시 DataGridview 행 색상 변경

public void UpdateColors(int state) 
{ 
    DefaultCellStyle.BackColor = m_ETBackColors[state]; 
    DefaultCellStyle.ForeColor = m_ETForeColors[state]; 
} 

나는 색상 변경을 테스트하는 순간 내 개체를 관찰, 그래서 수 없습니다 :이 클래스에서 , 나는이 방법을 가지고있다.

이제 작동하지 않습니다. 이전에 선택한 행이 파란색으로 유지되면 (선택한 경우처럼) 스크롤 할 때 셀 텍스트가 쌓입니다. DataGridView.Refresh()를 호출했지만 그 중 하나가 작동하지 않습니다.

데이터 원본에 바인딩되지 않은 datagridview를 추가해야합니다. 런타임 전에 얼마나 많은 열을 가지고 있는지 직접 알지 못하므로 수동으로 피드를 제공합니다.

누구든지 내가 뭘 잘못하고 있다고 말할 수 있습니까?

public void UpdateColors(int state) 
{ 
    DefaultCellStyle.BackColor = System.Drawing.Color.Yellow; 
    DefaultCellStyle.ForeColor = System.Drawing.Color.Black; 
} 

을하지만이 작동하지 않습니다 :

========== 업데이트 ==========

이 작동

public void UpdateColors(int state) 
{ 
    DefaultCellStyle.BackColor = m_ETBackColors[nEtattech]; 
    DefaultCellStyle.ForeColor = m_ETForeColors[nEtattech]; 
} 

:

System.Drawing.Color[] m_ETBackColors = new System.Drawing.Color[] { }; 
    System.Drawing.Color[] m_ETForeColors = new System.Drawing.Color[] { }; 

에는 배열 오버 플로우가 없습니다 : 그들은 생성자 매개 변수입니다.

답변

1

은 실수를 발견했다. 내가 사용 색상이 그렇게 만든 :

System.Drawing.Color.FromArgb(value) 

나쁜 것은 값이 게시물에 0
덕분에 알파 세트 색상을 나타내는 정수이다이다 : MSDN social post는, 내가 배운 셀 스타일 돈 알파가 255로 설정되어 있지 않으면 (RGB 색상 만 지원) ARGB 색상을 지원하지 않습니다.

그래서 나는 어떤 작품이, 사용 종료, 그러나 확실하게 더 우아한 방법이 :

System.Drawing.Color.FromArgb(255, System.Drawing.Color.FromArgb(value)); 
0

사용 CellFormatting 이벤트는 그것을 할 수 있습니다 :

확인
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    if (this.dataGridView1.Rows[e.RowIndex].Cells["SomeStuff"].Value.ToString()=="YourCondition") 
    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red; 
    else 
    this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White; 
} 
+0

가 이미 UpdateColors 내 행에 대한 사용자 정의 색상 속성을 설정하고 내 데이터 그리드의 CellFormatting 이벤트를 사용하여,이 시도는 하지만, 문제가 해결되지 않았다 '개인 무효 DataGridViewEtat_Technique_CellFormatting (객체 송신자 System.Windows.Forms.DataGridViewCellFormattingEventArgs E) { DataGridViewTechnicalStateRow 행 행 = [e.RowIndex] DataGridViewTechnicalStateRow 등; row.DefaultCellStyle.BackColor = row.BCL; row.DefaultCellStyle.ForeColor = row.FCL; }' – Rifu