2017-10-09 100 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()