我有 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); }
}
和两种形式:
感谢帮助
请您参考如下方法:
请试试这个代码方法:
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); }
}
