2017-06-01 44 views
0

我有一個窗體,用它來插入數據(Form2),數據顯示在另一個窗體(form1)存在的組合框中,我想刷新組合框在窗口2刷新表單後插入數據庫完成其他形式的combox c#

這裏插入後是我的代碼(form1中)

private void button1_Click(object sender, EventArgs e) 
{ 
    string libelle = textBox1.Text; 
    string cause = textBox2.Text; 
    string interval = textBox3.Text +'-'+ textBox4.Text; 

    d.open(); 
    int c = d.InsertPanne(libelle, cause, interval); 

    if (c == 1) 
    { 
     MessageBox.Show("Panne ajoutée avec succès"); 

     textBox1.Clear(); 
     textBox2.Clear(); 
     textBox3.Clear(); 
     textBox4.Clear(); 

     Form1 f = new Form1(); 
     f.Refresh(); 
    } 

    d.close(); 
} 

Form 1代碼:

public partial class Form1 : Form 
{ 
    add_panne p = new add_panne(); 
    DAO d = new DAO(); 

    public Form1() 
    { 
     InitializeComponent(); 
     List<panne> liste1 = d.getPannes(); 

     comboBox3.DataSource = new BindingSource(liste1,null); 
     comboBox3.DisplayMember = "nompan"; 
     comboBox3.ValueMember = "numpan"; 

     comboBox3.SelectedIndex = -1; 
    } 
} 

回答

0

如果你使用你的數據層的BindingList那麼它應該只是工作utomatically。

我附上了一個示例VS項目,演示了我相信你想要做的事情。

WindowsFormsComboRefresh VS project

+0

我用這comboBox3.DataSource =新的BindingSource(liste1,NULL);我怎樣才能做你所做的事請 –

+0

如何使用class.instance綁定來源 –

+0

mBindingSrc = new BindingSource(mData.MyData,「Name」); comboBox1.DataSource = mBindingSrc.DataSource; comboBox1.DisplayMember =「Name」; 我將MyData更改爲綁定列表而不是字符串。 這裏是食物類 公共類食物 { public string Name {get;組; } public string Category {get;組; } } – user3583535

0

克里特新功能例如UpdateFormsControl

private void UpdateFormsControls() 
{ 
List<panne> liste1 = d.getPannes(); 

    comboBox3.DataSource = null; 
    comboBox3.DataSource = liste1 ; 
    comboBox3.DisplayMember = "nompan"; 
    comboBox3.ValueMember = "numpan"; 

    comboBox3.SelectedIndex = -1; 
        textBox1.Clear(); 
        textBox2.Clear(); 
        textBox3.Clear(); 
        textBox4.Clear(); 

} 

只需調用此函數刷新組合框的項目...

private void button1_Click(object sender, EventArgs e) 
      { 
       string libelle = textBox1.Text; 
       string cause = textBox2.Text; 
       string interval = textBox3.Text +'-'+ textBox4.Text; 

       d.open(); 
       int c = d.InsertPanne(libelle, cause, interval); 
       if (c == 1) 
       { 
        MessageBox.Show("Panne ajoutée avec succès"); 

    //Call Update function to update values 
UpdateFormsControls(); 
        Form1 f = new Form1(); 
        f.Refresh(); 


       } 

       d.close(); 

      } 
+0

'Form1 f = new Form1()'---懷疑作品 –

相關問題