CSharp - 如何从Form2按钮单击事件调用Form1加载事件

  显示原文与译文双语对照的内容
0 0

我有两个简单的Windows form:FormA 和 FormB 。
我的格式中有一个简单的datagridView,并显示我的简单表信息,我使用这里代码以格式加载事件显示我的信息:

 string selectcommand ="select *from WhiteList";
 SqlCeConnection connection = new SqlCeConnection(conString);
 SqlCeDataAdapter adaptor = new SqlCeDataAdapter(selectcommand, conString);
 DataSet ds = new DataSet();
 adaptor.Fill(ds,"test");
 DataTable table = ds.Tables["test"];
 dataGridView1.DataSource = table;


在DatagridView中显示信息以及用户单击gridView行时,打开FormB并在FormB文本框中显示DatagridView字段,在FormB中:

SqlCeConnection connection = new SqlCeConnection(conString);
 connection.Open();
 SqlCeCommand cmd = new SqlCeCommand("Update WhiteList set Name=@behi,Count=@rezi where id=@rezii", connection);
 cmd.Parameters.AddWithValue("@behi", textBox1.Text);
 cmd.Parameters.AddWithValue("@rezi", Convert.ToInt32(textBox2.Text));
 cmd.Parameters.AddWithValue("@rezii", Convert.ToInt32(label1.Text));
 cmd.ExecuteNonQuery();


若要更新我的简单表,但在单击FormB上的更新按钮时,在形式的datagridView刷新中,如何执行这里操作?

时间:原作者:2个回答

0 0
public partial class FirstForm: Form
{
public FirstForm()
{
 InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
 SecondForm form2 = new SecondForm (this);
 SecondForm. Show();
}
public void UpdateDataGrid()
{
//add your code here
}
}
public partial class SecondForm : Form
{
private FirstForm form1;
public SecondForm ()
{
 InitializeComponent();
}
public SecondForm (FirstForm form1)
 : this()
{
 this.form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
 form1.UpdateDataGrid();
}
}
原作者:
...