2013-05-22 25 views
0

我有一個表單,其中包含一個過濾的datagridview(隱藏條目標記「關閉」)基於MySQL數據庫中的數據集和tableadapter。我被困在一個部分,涉及啓動一個對話框的形式,其中包含另一個datagridview基於相同的數據集,顯示錶中的所有條目。我希望能夠使用複選框列標記選擇那些「關閉」,單擊「關閉這些記錄」按鈕,關閉該對話框並將這些更改反映在過濾的datagridview中。從c#中修改datagridview MySQL更新

我試過多種方法來實現這一點,並沒有運氣。基本上,最接近的嘗試導致了空數據集時,我返回到過濾datagridview的....

我過濾的datagridview在這裏填寫:

this.dtClientTableAdapter.FillBy(this.DS.dtClient); 

的對話框這裏展開:

private void closeToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     CloseAgreement dlgCloseAgree = new CloseAgreement(); 
     dlgCloseAgree.ShowDialog(); 
     refreshRecords(); 
    } 

未過濾的DataGridView顯示在對話框中,在這裏填寫:

this.dtClientTableAdapter.Fill(this.DS.dtClient); 

要設置使用RowValidated事件的變化:

private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e) 
    { 
     DataTable changes = ((DataTable)dataGridView1.DataSource).GetChanges(); 

     if (changes != null) 
     { 
      MySqlCommandBuilder mcb = new MySqlCommandBuilder(mySqlDataAdapter); 
      mySqlDataAdapter.UpdateCommand = mcb.GetUpdateCommand(); 
      mySqlDataAdapter.Update(changes); 
      ((DataTable)dataGridView1.DataSource).AcceptChanges(); 
     }   
    } 

一切似乎直到關閉對話框做工精細。當以第一種形式返回到已過濾的datagridview時,datagridview將爲空,並通過重新填充tableadapter進行刷新是平淡無奇的。在調試時,關閉解釋空datagridview的對話框表單時,整個數據集爲空。有趣的是,當對話框關閉並且沒有進行任何更改時,第一個表格中的過濾數據網格仍然處於完好狀態。沒有可行的結果嘗試了其他幾種不同的方法。

我已經省略了設計器聲明,但如果需要澄清,我可以編輯該問題。

我必須俯視一些簡單的東西。這甚至是正確的方法嗎?任何幫助表示讚賞。

回答

1

我想通了......第一個DGV是更加開放的記錄的儀表板。第二個dgv是存儲在數據庫中的所有列表。我希望能夠「打開」和「關閉」記錄,並且不會在儀表板中看到「已關閉」的記錄,但仍然希望能夠在某個時間點打開它們並記錄所有以前關閉的記錄。我的問題是我試圖爲這兩個dgv使用相同的數據集。一旦我創建了一個獨立的數據集並查詢了數據庫,我就能夠進行更改,更新並返回到儀表板以查看更改。就我而言,這只是一個巨大的混亂。這是我創建的第一個mysql數據驅動的應用程序,因此使用mysql連接器進行了一些主要學習。儘管謝謝你的幫助。

0

在主DataGridView上過濾打開/關閉記錄的另一種方法是提供過濾器複選框並簡單地過濾數據。

這裏是一個最原始的例子:

using System; 
using System.Data; 
using System.Windows.Forms; 

class Form1 : Form 
{ 
    DataGridView dgv; 
    CheckBox hideCloseCheckBox; 
    BindingSource bindingSource; 

    public Form1() 
    { 
     /* 
     * Add a DataGridView with a simulated DataSet 
     * 
     * Note that we add a BindingSource between the DataSource member of the DGV and the DataSet. 
     * We will use that BindingSource to filter the items. 
     */ 
     Controls.Add((dgv = new DataGridView 
     { 
      Dock = DockStyle.Fill, 

      AutoGenerateColumns = false, 
      Columns = 
      { 
       new DataGridViewTextBoxColumn { Name = "Name", DataPropertyName = "Name", HeaderText = "Name", AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill }, 
       new DataGridViewCheckBoxColumn { Name = "Open", DataPropertyName = "Open", HeaderText = "Open", AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader }, 
      }, 

      DataSource = (bindingSource = new BindingSource(new DataSet 
      { 
       Tables = 
       { 
        new DataTable("Table1") 
        { 
         Columns = 
         { 
          new DataColumn("Name", typeof(string)), 
          new DataColumn("Open", typeof(bool)), 
         }, 

         Rows = 
         { 
          { "Item 1", true }, 
          { "Item 2", false }, 
          { "Item 3", true }, 
          { "Item 4", false }, 
          { "Item 5", true }, 
         }, 
        }, 
       }, 
      }, "Table1")), 
     })); 

     /* 
     * Add a "Hide closed records" checkbox 
     */ 
     Controls.Add((hideCloseCheckBox = new CheckBox 
     { 
      Dock = DockStyle.Top, 
      Text = "Hide closed records", 
      Padding = new Padding(20, 0, 0, 0), 
     })); 

     /* 
     * When the user clicks that checkbox, we change the Filter on the BindingSource 
     * 
     * See http://msdn.microsoft.com/en-us/library/cabd21yw.aspx for details on filter expressions. 
     */ 
     hideCloseCheckBox.Click += (s, e) => 
      { 
       if (hideCloseCheckBox.Checked) 
        bindingSource.Filter = "Open = true"; 
       else 
        bindingSource.Filter = ""; 
      }; 

     /* 
     * The problem we have now is that when the user toggles the Open checkbox, the record doesn't 
     * dissappear from view when the filter is enabled. This is because triggering the checkbox 
     * doesn't change the row data until after the row is committed. This normally happens when 
     * the user leaves the row. If you're allowing other editing on the row, this may be the 
     * desired behavior as you don't want the row to vanish mid-edit. However, if you do, you 
     * can force the issue by causing a checkbox toggle to commit the row. 
     */ 
     dgv.CellContentClick += (s, e) => 
      { 
       // If the "Open" column is clicked 
       if (dgv.Columns[e.ColumnIndex].Name == "Open") 
       { 
        // Trigger EndEdit on the dgv and if that succeeds, trigger it on the bindingSource 
        if (dgv.EndEdit()) 
         bindingSource.EndEdit(); 
       } 
      }; 
    } 

    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Form1()); 
    } 
} 
+0

那麼過濾並不是真正的問題。當編輯和提交時,問題出現在二級或子級datagridview中。當返回到主數據網格視圖時,它是空的,因爲由於某種原因數據集已被設置爲空。然後出於某種原因,我無法手動重新加載數據集,當主窗體在與輔助datagridview關閉後的對話框激活時。有關於此的任何想法? – user2409901

+0

@ user2409901沒有更多的代碼就無法猜測。我不明白爲什麼要用相同的數據向用戶展示兩個DGV。通常你會有一個DGV和一個編輯記錄對話框,讓他們只能從列表中編輯一條記錄。在這種情況下,您可以將包含該記錄的DataTableRow傳遞給對話框,並直接編輯已經綁定到DGV的行的內容。 – Tergiver