【发布时间】:2017-10-09 20:52:32
【问题描述】:
我用两个 froms(form1,form2) 编写了一个程序。Form1 有 datagridview,而 Form2 我用它将数据传递到数据库(我在 datagridview 中看到了它们)。我希望当我按下 form2 中的保存按钮时,同时在 form1 中看到 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);
【问题讨论】:
标签: c# mysql datagridview windows-forms-designer