gpt4 book ai didi

c# - 我可以将多个值从一种形式返回到另一种形式吗

转载 作者:太空宇宙 更新时间:2023-11-03 21:31:08 24 4
gpt4 key购买 nike

我有 2 个表单,第一个 form4 是一个 dataGridView 包含来自数据库的信息,当我点击“新建”按钮时,form5 显示我应该填写的公式,然后点击“添加”按钮,最后,我得到的结果应该出现在Form4的datagridView中。那可能吗?这是我的代码:

private void button2_Click(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand sql = new SqlCommand("insert into journal values('" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox2.SelectedItem.ToString() + "','" + comboBox1.SelectedItem.ToString() + "','" + checkBox1.Checked.ToString()+"','"+checkBox2.Checked.ToString()+"');", connection);
int o = sql.ExecuteNonQuery();
MessageBox.Show(o + " Le Fichier journal a ?t? ajout? avec succ?s");
connection.Close();
textBox1.Text = "";
textBox2.Text = "";
if(textBox1.Text ==" ")
{
MessageBox.Show("La saisie du code Journal est obligatoire!!");
}
affich();             
}
 
private void affich()
{
try
{
pat = connectionString;
req = "SELECT * FROM journal";
con = new SqlConnection(pat);
con.Open();
dr = new SqlDataAdapter(req, con);
dr.Fill(ds, "journal");
Form4 Form4 = new Form4();//ceci va creer une nouvelle instance je ne veux pas avoir une nouvelleFen?te
Form4.journalDataGridView.DataSource = ds.Tables["journal"];
Form4.Show();
}
catch (Exception e) { MessageBox.Show("Base de donn?es non trouv?e", e.Message); }
}        
    

和两种形式: enter image description here

感谢帮助

最佳答案

请试试这个代码方法:

private void affich()
{
try
{
pat = connectionString;
req = "SELECT * FROM journal";
con = new SqlConnection(pat);
con.Open();
dr = new SqlDataAdapter(req, con);
dr.Fill(ds, "journal");
Form4 obj= new Form4();//ceci va creer une nouvelle instance je ne veux pas avoir une nouvelleFenête
obj.journalDataGridView.DataSource = ds.Tables["journal"];
Form4.Show();
}
catch (Exception e) { MessageBox.Show("Base de données non trouvée", e.Message); }
}

关于c# - 我可以将多个值从一种形式返回到另一种形式吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24158656/

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