2013-03-19 2 views
0

양식이 추가 된 콘솔 응용 프로그램이 있습니다.DataGrid보기에서 행 색상을 변경하는 방법

이 양식에는 모든 속성이 기본값으로 남겨진 DataGrid보기가 있습니다. 이것은 '아무튼

public void ShowAnalysisData(List<Company> analysisData) 
{ 
    analysisGridView.DataSource = analysisData; 

    UpdateGridStyle(); 

} 

private void UpdateGridStyle() 
    { 
     foreach (DataGridViewRow row in analysisGridView.Rows) 
     { 
      string RowType = row.Cells[0].Value.ToString(); 

      if (RowType == "Insert") 
      { 
       row.DefaultCellStyle.BackColor = Color.Green; 
       // row.DefaultCellStyle.ForeColor = Color.Black; 
      } 
      else if (RowType == "Update") 
      { 
       row.DefaultCellStyle.BackColor = Color.Yellow; 
       // row.DefaultCellStyle.ForeColor = Color.Black; 
      } 
      else 
      { 
       row.DefaultCellStyle.BackColor = Color.Gray; 
       // row.DefaultCellStyle.ForeColor = Color.Black; 
      } 
     } 

:이 코드가 형태로

CompanyActions objCompanyActions = new CompanyActions(); 

List<Company> analyzedData = new List<Company>(); 

List<Company> CompaniesFromExternalSource = objCompanyActions.GetExternalCompanyData(@"company.csv"); 

analyzedData = objCompanyActions.Compare(CompaniesFromExternalSource); 
AnalysisForm objAnalysisForm = new AnalysisForm(); 

objAnalysisForm.ShowAnalysisData(analyzedData); 
Application.Run(objAnalysisForm); 

: Program.cs에서

나는이 Main 메서드에 코드를 일하지 마라. 그리드는 여전히 각 행에 대한 기본 흰색 배경을 유지합니까? 여기서 내가 뭘 잘못하고 있니?

감사합니다.

답변

0

잘못된 시간에 배경색을 변경하는 방법을 사용하고 있다고 생각합니다. 다음과 같이 CellFormatting에 배치 해보십시오.

private void analysisGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    DataGridViewRow row = analysisGridView.Rows[e.RowIndex]; 
    string RowType = row.Cells[0].Value.ToString(); 

    if (RowType == "Insert") 
    { 
     row.DefaultCellStyle.BackColor = Color.Green; 
     // row.DefaultCellStyle.ForeColor = Color.Black; 
    } 
    else if (RowType == "Update") 
    { 
     row.DefaultCellStyle.BackColor = Color.Yellow; 
     // row.DefaultCellStyle.ForeColor = Color.Black; 
    } 
     else 
    { 
     row.DefaultCellStyle.BackColor = Color.Gray; 
     // row.DefaultCellStyle.ForeColor = Color.Black; 
    } 
} 
+0

그래도 효과가 있지만 눈금이 스크롤되면 눈에 띄는 지연이 있습니다! 이것이 일어나는 이유는 무엇입니까? – Codehelp

+0

완전히 아래로 스크롤 한 다음 다시 올리면 그리드가 여전히 지연됩니까? –

+0

또한 서버 측 값보다는 셀 값을보고 있기 때문에 jquery를 사용하여이 작업을 수행하는 것이 좋습니다. 더 빠른 해결책이 될 수 있습니다. –

관련 문제