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

时间:2014-09-14 17:59:46

标签: c#

我有两个简单的窗口形式:FormA和FormB 在我的FormA中有一个简单的datagridView我显示了我的简单表信息,我使用此代码在FormA加载事件中显示我的信息:

 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 Row,打开FormB并在FormB textBox中显示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上单击FormB上的Update按钮时,在FormA datagridView刷新中,我该怎么做?

1 个答案:

答案 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();
}
}