2009-10-02 162 views
2

我開發了一個Windows窗體應用程序,它有一個複選框的數據網格。如果我按下一個按鈕,那麼我想要獲取所有已檢查的行並對它們執行一些操作。 問題是,按下按鈕時,它無法識別最新的複選框操作。這就像落後一步。Datagrid複選框單元格

我的代碼是:

private void copyToToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     dataGridView1.ClearSelection(); 
     DialogResult result = this.folderBrowserDialog1.ShowDialog(); 
     if (result == DialogResult.OK) 
     { 
      string foldername = this.folderBrowserDialog1.SelectedPath; 
      foreach (DataGridViewRow row in dataGridView1.Rows) 
      { 
       if (row.Cells[0].Value != null) 
       { 
        if ((bool)row.Cells[0].Value == true) 
        { 
         try 
         { 
          string[] vars = row.Cells[1].Value.ToString().Split('\\'); 
          System.IO.File.Copy(row.Cells[1].Value.ToString(), foldername + @"\" + vars[vars.Length - 1], true); 
         } 
         catch (Exception ex) 
         { 
          MessageBox.Show(ex.ToString()); 
         } 
        } 
       } 
      } 
     } 
    } 

數據是從SQL查詢 - 平常的東西。

+0

請提供一些更多相關信息和代碼。上面的內容並沒有太多幫助。 – 2009-10-02 10:14:29

回答

1

羅蘭,

原因可能是因爲數據尚未提交到源。 DataGridView的工作方式是「綁定」到源,並保持數據同步。但是,DataGridView中所做的更改不會立即更新DataGridView。它被認爲是'骯髒'。

嘗試在更改後檢查原始數據源,也可以嘗試調用BindingContext(..)。EndCurrentEndit();或者類似的嘗試,並確保數據承諾。

+0

添加dataGridView1.EndEdit();解決了問題 坦克! – Woland 2009-10-02 11:11:18