2017-10-09 3 views
0

私は2つのfroms(form1、form2)で1つのプログラムを作成しました.Form1にはdatagridviewがあり、Form2はデータをデータベースに渡します(datagridviewで見た)。私は私がフォーム2の保存ボタンを押したときに、同じように、現在のように、リフレッシュバウンスを外してform1でdatagridviewの値を見たかった。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

私はあなたの問題を理解していれば知らないけどただもしあなたと仮定することができます

次のコードは、ボタンを保存しています保存ボタンを押すと、フォーム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
コピーで

この1とし、この1 公共System.Windows.Forms.DataGridView GridOgrenciを入れ

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); 
     } 

を貼り付け; //このコードは、フォーム1ができますdatagridviewを他のフォームと共有します。

公共部分クラスフォーム1:あなたの後 フォーム

とあなたのフォームで2 をForm1としてあなたのForm_Load

FRM = Application.OpenForms [ "をForm1"]にこのコードを置く; //これはあなたのForm1

して、もう一度あなたの後

でのDataGridViewプロパティを使用できるようになります:

公共部分クラスフォーム2:あなたは私はあなたのフォーム1

のForm1 FRM =新しいForm1の

形2

サンプルであなたのDataGridViewを使用できるようにフォーム

に宣言を置きます

このコードの後に​​:

DialogResult dialog = MessageBox.Show( "Saved"、 "Saved"、MessageBoxButtons.OK);

FRMを置きます。DataGridRefresher(); //このコードは、DataGridView内のデータを更新します。

私は説明があまり良くない場合は申し訳ありませんでしたら返信してください。私はちょうど彼らの名前ではないコードで学習していて、また私の英語の文法のために残念これもあまり良くない。

0

Form1とForm2の間に接続(親子関係)があれば簡単です。

Public ParentFrm as Form1 

: - VB.NET

フォーム2:以下れる

一つの方法は、(多くの場合、監獄島、私はそれで最も「セクシー」な方法カウントし、年によってそれをテストした)、それを行うには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; 

我々はForm2の中ParentFrmを設定しているので、あなたが戻って通信し、Form1にこの方法を更新機能を呼び出すことができます - VB.NET:

ParentFrm.UpdateForm1FromDatabasePublicFunction() 

のC#:

ParentFrm.UpdateForm1FromDatabasePublicFunction() 
関連する問題