2012-01-29 89 views
0

您好我有一個datagridview其第一列(索引= 0)是一個複選框列。我想在單元格被選中或未選中時更新標籤。我的功能似乎工作正常,但它不會更新標籤,直到該列中的下一個單元格被更改。捕獲單元格列單擊並執行事件

當窗體加載所有行時,檢查,所以說4或4.我取消選中一行,標籤不更新。我不檢查另一行,然後它說3,所以你看到它在後面一步工作。

我已經嘗試了幾種不同的DataGridViewCellEvents,像CellValueChanged,CellStateChanged,CellEndEdit,但他們都採取行動的方式。我仍然需要有一個DataGridViewCellEventArgs e,所以我可以檢查列。

有什麼建議?

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. 
       // 
      } 
     } 
} 

編輯:

演示解決方案。

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 2012-01-29 01:41:04

+0

好的,那很奇怪。我只是創建了一個演示sln和片段的作品。顯示MessageBox ...什麼東西在您的項目中不起作用? – stylefish 2012-01-29 01:51:51

+0

是的消息框會顯示,但我需要增加或減少一個int當複選框被選中/取消選中 - 基本上只是顯示檢查了多少行。讓我嘗試重寫我的代碼以使用你的方法,因爲我之前設置的工作方式有點不同 – ikathegreat 2012-01-29 01:54:35

相關問題