简体   繁体   中英

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

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.

is this the code in your Form 1 that will display the data in your datagridview right?

 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
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. after your :

public partial class Form 1: Form

and in your Form 2 put this code in your Form_load

frm = Application.OpenForms["Form1"] as Form1;//this will allow you to use the datagridview property in your form1

and again

after your :

public partial class Form 2: Form

put

a declaration so you can use your datagridview in form 2

sample i your form 1

Form1 frm = new Form1

then

after this code:

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

put

frm.DataGridRefresher();//this code will refresh the data in your 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.

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:

Public ParentFrm as Form1  

C#:

public Form1 ParentFrm;  

Form 1 - in it's running instance - 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;

Since we set the ParentFrm in the Form2, you can communicate back and call update function in Form1 this way - VB.NET:

ParentFrm.UpdateForm1FromDatabasePublicFunction()

C#:

ParentFrm.UpdateForm1FromDatabasePublicFunction()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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