2016-11-03 13 views
0

DataGridView에서 CellFormatting 메서드를 사용하여 해당 내용을 기반으로 행의 색을 다시 지정하는 데 문제가 있습니다. 이것은 DGV의 첫 번째 행이 true를 반환 할 때까지 대부분의 경우에 잘 작동합니다.이 경우 DGV의 모든 행은 빨간색으로 표시됩니다.C# DataGridView 행 색 지정

private void MyData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    DataGridViewRow theRow = MyData.Rows[e.RowIndex]; 
     if (theRow.Cells[4].Value.ToString() == "1") 
     { 
      ChangeRowColor(theRow, Color.PaleVioletRed); 
     } 
} 

private void ChangeRowColor(DataGridViewRow r, Color c) 
{ 
    r.DefaultCellStyle.BackColor = c; 
} 

편집 : 솔루션 :

private void MyData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
    { 
     DataGridViewRow theRow = MyData.Rows[e.RowIndex]; 
     if ((int)theRow.Cells[4].Value == 1) 
     { 
      e.CellStyle.BackColor = Color.PaleVioletRed; 
     } 
} 
+0

나에게 서식을 지정해 주셔서 감사합니다. newb입니다. – Krisisonfire

+0

else if 절 이후에 else {ChangeRowColor (theRow, Color.White);}'를 추가하면 도움이됩니까? – Pikoh

+0

샘플 데이터를 제공 할 수 있습니까? – Berkay

답변

0

당신은 이벤트에 CellStyle에 배경색을 지정해야합니다

여기 내 코드입니다.

+0

그게 내가하는 일이 아닌가? – Krisisonfire

+0

인수 e에는 CellStyle 속성이 있습니다. 그것은 당신이 거기에있는 세포와 줄의 스타일입니다. –

+0

너 천재적 인 천재 야, 나는 열등 해. – Krisisonfire