2013-02-08 2 views
0

양식과 DataGridView 컨트롤을 기반으로하는 응용 프로그램을 만들고 있습니다.값에 따라 셀 스타일을 변경 하시겠습니까?

나는 데이터베이스에서 정보를 바인딩하고 있는데, 지금하려는 것은 "Urgent","Haute","Normale" 일 수있는 값에 따라 열 속성의 글꼴 스타일과 색을 변경하는 것입니다.

여기 제가 사용하고 있지만 작동하지 않는 코드가 있습니다. 아래 코드에서 잘못된 점이 무엇인지 말해 줄 수 있습니까?

코드 :

private void ColorizeCellsbyValue() { 

     DataGridViewCellStyle BoldRed = null; 
     BoldRed = new DataGridViewCellStyle(); 
     BoldRed.Font = new Font("Tahoma", 9, FontStyle.Bold); 
     BoldRed.ForeColor = Color.Red; 

     DataGridViewCellStyle Red = null; 
     Red = new DataGridViewCellStyle(); 
     Red.ForeColor = Color.Red; 

     DataGridViewCellStyle Black = null; 
     Black = new DataGridViewCellStyle(); 
     Black.ForeColor = Color.Black; 
     string priority; 
     foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 
      priority = row.Cells[3].Value.ToString(); 
      switch (priority) 
      { 
       //Change font 
       case "Urgent": 
        row.Cells[3].Style = BoldRed; 
        break; 
       case "Haute": 
        row.Cells[3].Style = Red; 
        break; 
       case "Normale": 
        row.Cells[3].Style = Black; 
        break; 
       default: 
        row.Cells[3].Style = Black; 
        break; 
      } 
     } 
} 

답변

1

이 작동합니다 ForeColor 사용

row.Cells[3].Style.ForeColor = <Color>; 

에 대한

row.Cells[3].Style.BackColor = <Color>; 

사용해보십시오. 해피 코딩!

+0

+1을 사용하려고하지만 불행하게도 영업 이익은 _ "글꼴 스타일을 변경"싶어 : D' – spajce

+0

@ spajce for Fore color : – Ravia

+0

당신의 답을 형식화하고 싶지 않습니다. ': D' – spajce

1

Column 속성을 디자인하려면 DataGridViewCellStyle을 만들 필요가 없습니다.

 foreach (DataGridViewRow rows in dataGridView1.Rows) 
     { 
      if (rows.Cells[3].RowIndex % 2 == 0) 
      { 
       rows.Cells[3].Style.Font = new Font("Tahoma", 9, FontStyle.Bold); 
       rows.Cells[3].Style.BackColor = Color.Red; 
      } 
      else 
      { 
       rows.Cells[3].Style.Font = new Font("Arial", 9, FontStyle.Regular); 
       rows.Cells[3].Style.BackColor = Color.Blue; 
      } 
     } 

내 간단한 예제 및 주요 문제에 대한 내 대답을 알아 내기 위해 노력입니다`_ .EditedFormattedValue.ToString()

 foreach (DataGridViewRow row in dataGridView1.Rows) 
     { 
      priority = row.Cells[3].EditedFormattedValue.ToString(); 
      switch (priority) 
      { 
       //Change font 
       case "Urgent": 
        row.Cells[3].Style = BoldRed; 
        break; 
       case "Haute": 
        row.Cells[3].Style = Red; 
        break; 
       case "Normale": 
        row.Cells[3].Style = Black; 
        break; 
       default: 
        row.Cells[3].Style = Black; 
        break; 
      } 
     } 
관련 문제