2017-10-09 101 views
0

我已經做了兩個froms(form1,form2)的一個programma.Form1有datagridview和我用它來傳遞數據到數據庫(我在datagridview中看到它們)的Form2。我希望當我按下form2中的保存按鈕時,同時在form1的datagridview中看到刷新的butoon(就像現在這樣)的值。在datagridview中的自動顯示更改

下面的代碼保存按鈕:

 string cdata = "Server=127.0.0.1;Database=liveriservis;Port=3306;Uid=root;Pwd=axmn1336;"; 
     MySqlConnection con = new MySqlConnection(cdata);   
     Querypelatis = "insert into liveriservis.pelatis(Όνομα,Επώνυμο,Κινητό,Σταθερό,date) values('" + this.name.Text + "','" + this.eponimo.Text + "','" + this.kinito.Text + "','" + this.stathero.Text + "','" + this.dateTimePicker2.Text + "');";    
     Queryteliko = "insert into liveriservis.teliko(Date,Όνομα,Επώνυμο,Σταθερό,Κινητό,ΠρόβλημαPc,ΠρόβλημαPc1,ΜοντέλοLaptop,ΠρόβλημαLaptop,ΠρόβλημαLaptop1,ΕξαρτήματαLaptop,ΜοντέλοΚινητό,ΠρόβλημαΚινητό,ΠρόβλημαΚινητό1,Αρίθμηση,Τιμή)values ('" + this.dateTimePicker2.Text + "','" + this.name.Text + "','" + this.eponimo.Text + "','" + this.stathero.Text + "','" + this.kinito.Text + "','" + this.problimapc.Text + "','" + strpc + "','" + this.modelolaptop.Text + "','" + this.problimalaptop.Text + "','" + strlaptop + "','" + strlaptop1 + "','" + modelo + "','" + this.problimakinito.Text + "','" + strkinito + "','" + this.arithisi.Text + "','" + this.timi.Text + "') ;"; 
     Queryolokliromeno = "insert into liveriservis.olokliromeno(Date,Όνομα,Επώνυμο,Σταθερό,Κινητό,ΠρόβλημαPc,ΠρόβλημαPc1,ΜοντέλοLaptop,ΠρόβλημαLaptop,ΠρόβλημαLaptop1,ΕξαρτήματαLaptop,ΜοντέλοΚινητό,ΠρόβλημαΚινητό,ΠρόβλημαΚινητό1,Αρίθμηση,Τιμή)values ('" + this.dateTimePicker2.Text + "','" + this.name.Text + "','" + this.eponimo.Text + "','" + this.stathero.Text + "','" + this.kinito.Text + "','" + this.problimapc.Text + "','" + strpc + "','" + this.modelolaptop.Text + "','" + this.problimalaptop.Text + "','" + strlaptop + "','" + strlaptop1 + "','" + modelo + "','" + this.problimakinito.Text + "','" + strkinito + "','" + this.arithisi.Text + "','" + this.timi.Text + "') ;"; 


     MySqlCommand cmpelatis = new MySqlCommand(Querypelatis, con); 
     MySqlCommand cmteliko = new MySqlCommand(Queryteliko,con); 
     MySqlCommand cmolokliromeno = new MySqlCommand(Queryolokliromeno, con); 

     MySqlDataReader myReader; 
     try 
     { 
      con.Open(); 
      myReader = cmpelatis.ExecuteReader(); 
      con.Close(); 
      con.Open(); 
      myReader = cmteliko.ExecuteReader(); 
      con.Close();    
      con.Open(); 
      myReader = cmolokliromeno.ExecuteReader(); 

      DialogResult dialog= MessageBox.Show("Saved","Saved",MessageBoxButtons.OK); 
      if (dialog == DialogResult.OK) { this.Close(); } 
      //else if (dialog == DialogResult.No) { e.Cancel = true; } 
      while (myReader.Read()) 
      { 
       // Application.Exit(); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
      // Application.Exit(); 
     } 

    } 

而下面的代碼是刷新按鈕:

 string cdata = "Server=127.0.0.1;Database=liveriservis;Port=3306;Uid=root;Pwd=axmn1336;"; 
     condata = new MySqlConnection(cdata); 
     cmgrid = new MySqlCommand("select * from liveriservis.teliko", condata); 

     sda = new MySqlDataAdapter(); 
     sda.SelectCommand = cmgrid; 
     dset = new DataTable(); 
     sda.Fill(dset); 
     BindingSource bSource = new BindingSource(); 
     bSource.DataSource = dset; 
     dataGridView1.DataSource = bSource; 

     sda.Update(dset); 

回答

0

我不知道如果我理解你的問題,但讓我們只是假設,如果你將點擊保存按鈕,您在Form 2中輸入的數據將顯示在Form1的datagridview中。

這是您的表單1中的代碼,它將顯示datagridview中的數據嗎?

string cdata = "Server=127.0.0.1;Database=liveriservis;Port=3306;Uid=root;Pwd=axmn1336;"; 
    condata = new MySqlConnection(cdata); 
    cmgrid = new MySqlCommand("select * from liveriservis.teliko", condata); 

    sda = new MySqlDataAdapter(); 
    sda.SelectCommand = cmgrid; 
    dset = new DataTable(); 
    sda.Fill(dset); 
    BindingSource bSource = new BindingSource(); 
    bSource.DataSource = dset; 
    dataGridView1.DataSource = bSource; 

    sda.Update(dset); 

所以試試這個。

在Form1中
拷貝一個粘貼

public void DataGridRefresher() 
    { 
    condata = new MySqlConnection(cdata); 
    cmgrid = new MySqlCommand("select * from liveriservis.teliko", condata); 
    sda = new MySqlDataAdapter(); 
    sda.SelectCommand = cmgrid; 
    dset = new DataTable(); 
    sda.Fill(dset); 
    BindingSource bSource = new BindingSource(); 
    bSource.DataSource = dset; 
    dataGridView1.DataSource = bSource; 

    sda.Update(dset); 
     } 

然後把這個 公共System.Windows.Forms.DataGridView GridOgrenci; //這個代碼將允許表1將datagridview分享給其他表單。 後您:

公共部分類形式1:表單

並在表格2 在Form_load中把這個代碼

FRM = Application.OpenForms [ 「Form1中」]作爲Form; //這將允許你

,並再次

後您使用的datagridview屬性在Form1中:

公共部分類表2:表

一個聲明,使您可以使用您的datagridview在形式上2

樣品我你的表格1個

Form1中FRM =新Form1中

then

此代碼後:

DialogResult dialog = MessageBox.Show(「Saved」,「Saved」,MessageBoxButtons.OK);

FRM。DataGridRefresher(); //這段代碼將刷新你的datagridview中的數據。

只是回覆如果它工作抱歉,如果我不太好解釋。我只是在代碼中學習,而不是以他們的名字學習,對於我的英語語法我也不太好。

0

這很簡單,只要您有Form1和Form2之間的連接(即父子關係)。

一種方式做到這一點(通常condemed,但是我算什麼最 「性感」 的方式,並通過多年的測試吧)以下:

表格2 - VB.NET:

Public ParentFrm as Form1 

C#:

public Form1 ParentFrm; 

表格1 - 在它的運行實例 - VB.NET:

Dim NewInstanceOfForm2 as New Form2 
NewInstanceOfForm2.ParentFrm = Me ' this is to ensure you can talk back to correct instance of parent form 

C#:

Form2 NewInstanceOfForm2 = new Form2(); 
NewInstanceOfForm2.ParentFrm = this; 

由於我們在窗體2設置ParentFrm,你可以回去溝通,並調用更新功能在Form1中這樣 - VB.NET:

ParentFrm.UpdateForm1FromDatabasePublicFunction() 

C#:

ParentFrm.UpdateForm1FromDatabasePublicFunction()