简体   繁体   English

自动显示datagridview中的更改

[英]Auto show change in datagridview

I have made one programma with two froms(form1,form2).Form1 has the datagridview and the Form2 i use it to pass the data to database(wich i saw them in datagridview). 我已经用两个froms(form1,form2)编写了一个程序.Form1有datagridview,而Form2我用它来将数据传递给数据库(直到我在datagridview中看到了它们)。 I want when i press tha save button in the form2 the same time saw the values in the datagridview at the form1 with out refresh butoon(like that is now). 我想当我在form2中按tha保存按钮的同时看到form1的datagridview中的值,而没有刷新按钮(像现在这样)。

The following code is the save button: 以下代码是保存按钮:

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

    }

And the following code is the refresh button: 以下代码是刷新按钮:

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

i dont know if i understand your problem but lets just assume that if you will hit the save button the data that you entered in Form 2 will display in datagridview in Form1. 我不知道我是否理解您的问题,但仅假设如果您按下保存按钮,则您在Form 2中输入的数据将显示在Form1的datagridview中。

is this the code in your Form 1 that will display the data in your datagridview right? 这是您的Form 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);

so try this. 所以试试看

in your Form1 在您的Form1中
copy this one and paste 复制这个并粘贴

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

then put this one public System.Windows.Forms.DataGridView GridOgrenci;//this code will allow the Form 1 to share the datagridview to other forms. 然后将这个公共System.Windows.Forms.DataGridView GridOgrenci; //此代码将允许Form 1与其他表单共享datagridview。 after your : 在您之后:

public partial class Form 1: Form 公开部分课程表格1:表格

and in your Form 2 put this code in your Form_load 然后在您的Form 2中将此代码放入Form_load

frm = Application.OpenForms["Form1"] as Form1;//this will allow you to use the datagridview property in your form1 frm = Application.OpenForms [“ Form1”]作为Form1; //这将允许您在form1中使用datagridview属性

and again 然后再次

after your : 在您之后:

public partial class Form 2: Form 公开部分课程表格2:表格

put

a declaration so you can use your datagridview in form 2 声明,以便您可以在表格2中使用datagridview

sample i your form 1 样品我你的表格1

Form1 frm = new Form1 Form1 frm =新的Form1

then 然后

after this code: 此代码后:

DialogResult dialog= MessageBox.Show("Saved","Saved",MessageBoxButtons.OK); DialogResult dialog = MessageBox.Show(“ Saved”,“ Saved”,MessageBoxButtons.OK);

put

frm.DataGridRefresher();//this code will refresh the data in your datagridview. frm.DataGridRefresher(); //此代码将刷新datagridview中的数据。

just reply if it worked sorry if i am not too good in explanation. 如果我的解释不太好,请回复是否有效。 i am just learning in code not in their names and sorry also for my english grammar im not so good in this also. 我只是在学习代码而不是他们的名字,对不起我的英语语法也不太好。

That is simple, provided you have connection (ie parent-child relation) between Form1 and Form2. 这很简单,只要您在Form1和Form2之间建立连接(即父子关系)。

One way to do it (often condemed, but I count it the most "sexy" way and tested it by years) is following: 一种方法(通常被谴责,但我认为它是最“性感”的方法,并经过几年的测试):

Form 2 - VB.NET: 表格2-VB.NET:

Public ParentFrm as Form1  

C#: C#:

public Form1 ParentFrm;  

Form 1 - in it's running instance - VB.NET: 表格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#: C#:

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

Since we set the ParentFrm in the Form2, you can communicate back and call update function in Form1 this way - VB.NET: 由于我们在Form2中设置了ParentFrm,因此您可以通过Form1向后通信并调用更新功能-VB.NET:

ParentFrm.UpdateForm1FromDatabasePublicFunction()

C#: C#:

ParentFrm.UpdateForm1FromDatabasePublicFunction()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM