2016-11-03 29 views
0

私はDataGridViewに問題があります。私はCellFormattingメソッドを使用してその内容に基づいて行の色を変更しています。これは、ほとんどの場合、DGVの最初の行が真を返すまでうまくいくように見えます。この場合、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 else節の後に 'else {ChangeRowColor(theRow、Color.White);}'を追加すると便利ですか? – Pikoh

+0

サンプルデータを提供できますか? – Berkay

答えて

0

あなたがイベントにCellStyleに背景色を割り当てる必要があります。

+0

それは私がやっていることではありませんか? – Krisisonfire

+0

引数eにはCellStyleというプロパティがあります。それはあなたがそこに持っているセルと行のスタイルです。 –

+0

あなたは驚異的な天才であり、私は劣っています – Krisisonfire