2016-07-30 5 views
0

dataGridRows.Cells[1]にはどれくらいの数字がありますか0があります。このコードをdataGridView1_RowPostPaintイベントに追加しました。データグリッドセル値がカウントされます

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) 
{ 
    int count=0; 

    if (dataGridView1.Rows.Count > 1) 
    foreach (DataGridViewRow row in dataGridView1.Rows) 
    { 
     count++; 
     foreach (DataGridViewCell cell in row.Cells) 
     { 
      if (Convert.ToInt32(cell) == 0) 
      { 
       label3.Text = count.ToString(); 

      } 
     } 
    } 
} 

私もこれで試してみました:wchichの

private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) 
{ 
    int count=0; 

    foreach (DataGridViewRow row in dataGridView1.Rows) 
    { 
     count++; 
     foreach (DataGridViewCell cell in row.Cells) 
     { 
      if (cell.Value.ToString() == "0") 
      { 
       label3.Text = count.ToString(); 

      } 
     } 
    } 
} 

いずれも正常に動作しません。最初のものは全く数えられません。 2番目のコードで、私にエラーが発生しましたAn exception of type 'System.NullReferenceException' occurred in skraper.exe but was not handled in user code

私にそれを手伝ってもらえますか?

+0

すべてのセルに数字「0」が何回表示されているか知りたいですか? – MasterXD

+0

セルの内容として「0」しかないセルがいくつあるか知りたい。 – audiophonic

答えて

1

ユーザーは自分自身で行を追加できますか?最後の行はコードで検出可能ですが、値はnullです。 NullReferenceExceptionがあります。あなたがすることはここにあります:

int zeros = 0; 

foreach (DataGridViewRow row in dataGridView1.Rows) // For every row 
    foreach (DataGridViewCell cell in row.Cells) // For every cell in the current row 
     if (cell.Value != null) // If cell's value is not null 
      if (cell.Value.ToString() == "0") // If cell's value is "0" 
       zeros++; // Increase count 

MessageBox.Show(zeros.ToString()); // Show result 

私はこれが役立つことを望みます。

+0

確かにそれは素晴らしい作品です。ありがとう:) – audiophonic