2012-01-29 8 views
0

はこんにちはは、その最初の列(インデックス= 0)のチェックボックス列でのDataGridViewを有します。私は、セルがチェックされ、チェックが外されたときにラベルを更新したい。私の関数はうまくいくようですが、その列の次のセルが変更されるまでラベルを更新しません。フォームの負荷がすべての行がチェックされたときに 捕捉セル列のクリックと実行イベント

ので、私は1行をオフにして、ラベルが更新されない4の4を言います。私は別の行のチェックを外し、それから3と言うので、それは一歩一歩前進しているのを見ます。

私はCellValueChanged、CellStateChanged、CellEndEditのようないくつかの異なるDataGridViewCellEventsを試してみたが、彼らはすべての発言のように行動。私はまだ列をチェックすることができますので、DataGridViewCellEventArgsを持っている必要があります。

提案がありますか?

private void fileGrid_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 
      if (e.ColumnIndex == 0) 
      { 
       int numberOfFiles = 0; 
       for (int i = 0; i < fileGrid.Rows.Count; i++) 
       { 
        Console.WriteLine(fileGrid[0, i].Value.ToString()); 
        if (fileGrid[0, i].Value.ToString() == "True") 
        { 
         numberOfFiles++; 
        } 
       } 
       numberOfFilesLabel.Text = numberOfFiles.ToString(); 
      } 
     } 

私はまだ自分の質問に答えることはできませんが、これは私が達成するために使用するものである:

private void fileGrid_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 
      if (e.ColumnIndex == 0) 
      { 
       int numberOfFiles = 0; 
       for (int i = 0; i < fileGrid.Rows.Count; i++) 
       { 
        Console.WriteLine(fileGrid[0, i].Value.ToString()); 
        if (fileGrid[0, i].Value.ToString() == "True") 
        { 
         numberOfFiles++; 
        } 
       } 

       if (fileGrid.IsCurrentCellDirty) 
        fileGrid.CommitEdit(DataGridViewDataErrorContexts.Commit); 

       if (fileGrid.CurrentCell.Value.ToString().Equals("True")) 
       { 

        numberOfFiles++; 
       } 
       if (fileGrid.CurrentCell.Value.ToString().Equals("False")) 
       { 

        numberOfFiles--; 
       } 
       numberOfFilesLabel.Text = numberOfFiles.ToString(); 
      } 
     } 

答えて

3

編集した値は、チェックボックスを変更した後に直接コミットされていないため、この問題が発生しました。あなたがエディタを離れるときにコミットします。 あなたが望む動作を取得するために、すぐに値をコミットする必要があります。私は見つけましたが、しようとしなかった 一つの方法は、このいずれかになります。

private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
     if (e.ColumnIndex == 0) 
     { 
      if (dataGridView2.IsCurrentCellDirty) 
       dataGridView2.CommitEdit(DataGridViewDataErrorContexts.Commit); 

      if (dataGridView2.CurrentCell.Value.ToString().Equals("True")) 
      { 
       MessageBox.Show("Now do the job while checked value changed to True."); 
       // 
       // Do the job here. 
       // 
      } 
     } 
} 

EDIT:

デモソリューション。

public partial class Form1 : Form 
{ 
    class MyClass 
    { 
     public bool Check { get; set; } 
     public string Name { get; set; } 
    } 

    public Form1() 
    { 
     InitializeComponent(); 

     List<MyClass> lst = new List<MyClass>(); 
     lst.AddRange(new[] { new MyClass { Check = false, Name = "item 1" }, new MyClass { Check = false, Name = "item 2" } }); 
     dataGridView1.DataSource = lst; 
    } 

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
    { 
     if (e.ColumnIndex == 0) 
     { 
      if (dataGridView1.IsCurrentCellDirty) 
       dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); 

      if (dataGridView1.CurrentCell.Value.ToString().Equals("True")) 
      { 
       MessageBox.Show("Now do the job while checked value changed to True."); 
       // 
       // Do the job here. 
       // 
      } 
     } 

    } 
} 
+0

ええと、同じ結果、申し訳ありません。 – ikathegreat

+0

ok、thats weird。私はちょうどデモslnを作成し、スニペットが動作します。 MessageBoxが表示されます...あなたのプロジェクトでは何が正しく動作しませんか? – stylefish

+0

はいメッセージボックスが表示されますが、私はチェックボックスが/未チェックチェックされているint型を増減する必要がある - 基本的には確認されている行数を示します。私が前に設定すると、少しは違っ – ikathegreat

関連する問題