2016-11-02 8 views
0

:すべての行がbool_badge =0を持って私は条件としたい

  • ForestGreen
  • 付きカラー: RED
  • すべての行と色がbool_badge=1を持っています

私はコードを持っていますが、特定のセルをクリックしたときに訂正されました。

マイコード:

foreach (DataGridViewRow dr in dataGridView1.Rows) 
     {    
      int row = this.dataGridView1.CurrentCell.RowIndex; 
      string valeur = dataGridView1[2, row].Value.ToString(); 

      if (valeur == "0") 
      { 
       dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red; 
      } 
      else 
      { 
       dataGridView1.DefaultCellStyle.SelectionBackColor = Color.ForestGreen; 
      } 
     } 

結果: 1)enter image description here `

2)enter image description here

しかし、私は、私は自分のアプリケーションを実行するときbool_badge 0または1の場合、テストを開始したい、と私はすべてのグリッドビューを持っています:色REDまたはForestGreen、

私はこのコードを試してみます:

for (int i = 0; i < dataGridView1.Rows.Count; i++) 
     {     
      string valeur = dataGridView1[2, i].Value.ToString(); 

      if (valeur == "0") 
      { 
       dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Red; 
      } 
      else 
      { 
       dataGridView1.DefaultCellStyle.SelectionBackColor = Color.ForestGreen; 
      } 
     } 

しかし、私にはエラーがあります!

これは、次のとおりです。

enter image description here

私はそれを解決することができますか?

非常に感謝し、

答えて

0

DatagridviewのCell_Formattingイベントを使用できます。

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
     { 
      if (dataGridView1.Columns[e.ColumnIndex].HeaderText == "bool_badge" && dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null) 
     // if the column is bool_badge and check null value for the extra row at dgv 
      { 
        if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "0") 
        { 
         dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red; 
        } 
        if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "1") 
        { 
         dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.ForestGreen; 
        } 
      } 
     } 

結果は、

enter image description here

ホープあなたがValue.ToString()をしないデバッグを支援するために、

+0

おかげで、 が、私はそれを試しに設定されていない未処理だったのが、結果はありません!任意の色!データ付きのDataGridView 他に何か不足していますか? – devtunis

+0

@devtunis直接コピーしないでください。デザインでセルの書式を設定します。 Datagridview-> f4 - >ライトニングボタンを押す - >セルを見つけてエバットをダブルクリックしてください。あなたはすでにこの方法でやっています。デバッグしましたか?私は、ダブルクリックでCellFormattingを設定 – Berkay

+0

、 と私は ます。private void dataGridView1_CellFormatting(オブジェクト送信者、DataGridViewCellFormattingEventArgs E) しかし、何のコードを作ります!色はありません!表示するだけです。 – devtunis

0

あなたが.Rowsと.Cellsプロパティを使用する必要があります。

string valeur = dataGridView1.Rows[i].Cells[2].Value.ToString(); 
+0

おかげで、 が、私は自分のアプリケーションを実行すると、何の色は表示されません!データとのDataGridViewの表示だけ、 と私は特定のセルにクリックしたときに、私は同じエラーがあります: とNullReferenceExceptionが オブジェクト参照がオブジェクトのインスタンスに – devtunis

0

を助けになります。ただ

var value = dataGridView_XXX.Rows[rowNumber].Cells[i].value; 

そして

if (value == null) display your row/column index 
else dataGridView_XXX.Rows[rowNumber].DefaultCellStyle.BackColor = xxx; 
関連する問題