gpt4 book ai didi

c# - 自动显示 datagridview 中的更改

转载 作者:行者123 更新时间:2023-11-29 18:20:14 26 4
gpt4 key购买 nike

我用两个表单(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);

最佳答案

我不知道我是否理解你的问题,但我们假设如果你点击保存按钮,你在 Form 2 中输入的数据将显示在 Form1 的 datagridview 中。

这是您的 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);

所以试试这个。

在您的 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);
}

然后放这个 public System.Windows.Forms.DataGridView GridOgrenci;//此代码将允许表单1将datagridview共享给其他表单。在你的:

之后

公共(public)分部类表格1:表格

以及您的表格 2将此代码放入您的 Form_load

frm = Application.OpenForms["Form1"] as Form1;//这将允许您在 form1 中使用 datagridview 属性

再一次

在你的:

之后

公共(public)分部类表格2:表格

声明,以便您可以在表单 2 中使用 datagridview

您的表格 1 示例

Form1 frm = 新Form1

然后

在此代码之后:

DialogResult 对话框= MessageBox.Show("已保存","已保存",MessageBoxButtons.OK);

放置

frm.DataGridRefresher();//此代码将刷新 datagridview 中的数据。

如果有效,请回复,如果我解释得不太好,抱歉。我只是在代码中学习,而不是在他们的名字中学习,也很抱歉我的英语语法我在这方面也不太好。

关于c# - 自动显示 datagridview 中的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46654780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com