如何在子表单关闭时刷新父表单上的datagridview

时间:2015-01-08 23:18:32

标签: c#

我想知道当frmUpdate打开为.showDialog关闭时,如何在frmClient上自动刷新我的datagridview。我尝试在frmUpdate表单关闭事件和frmClient加载时调用frmClient中的刷新按钮单击事件,但两者都没有工作。

      private void frmUpdate_FormClosing(object sender, FormClosingEventArgs e)
    {
    //     kryptonButton1_Click_1(null;null);
       frmClient_Load_1(null;null);
    }



  public void frmClient_Load_1(object sender, EventArgs e)
    {

       var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString;

        connection = new MySqlConnection(connectionString);

        if (this.OpenConnection() == true)
        {
            MySqlCommand sqlCmd = new MySqlCommand("sp_clientgridview", connection);
            sqlCmd.CommandType = CommandType.StoredProcedure;

            mySqlDataAdapter = new MySqlDataAdapter(sqlCmd);
            DataSet DS = new DataSet();
            mySqlDataAdapter.Fill(DS);
            sqlCmd.ExecuteNonQuery();
            kryptonDataGridView1.DataSource = DS.Tables[0];
            kryptonDataGridView1.Columns[0].Visible = false;
            kryptonDataGridView1.Columns[2].Visible = false;



        }


    }

3 个答案:

答案 0 :(得分:1)

您可以创建一个函数来加载客户端。

private void LoadClient()
{
    var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString;

    connection = new MySqlConnection(connectionString);

    if (this.OpenConnection() == true)
    {
        MySqlCommand sqlCmd = new MySqlCommand("sp_clientgridview", connection);
        sqlCmd.CommandType = CommandType.StoredProcedure;

        mySqlDataAdapter = new MySqlDataAdapter(sqlCmd);
        DataSet DS = new DataSet();
        mySqlDataAdapter.Fill(DS);
        sqlCmd.ExecuteNonQuery();
        kryptonDataGridView1.DataSource = DS.Tables[0];
        kryptonDataGridView1.Columns[0].Visible = false;
        kryptonDataGridView1.Columns[2].Visible = false;
}

private void frmClient_Load_1(object sender, EventArgs e)
{
  LoadClient();
}

//In Update button:
private void btnUpdate_Click(object sender, EventArgs e)
{
  frmUpdate frm = new frmUpdate();
  frm.ShowDialog()
  //refresh the datagridview by call again the LoadClient();
  LoadClient();
}

应该这样做:)快乐的编码!

答案 1 :(得分:0)

也许你忘了调用刷新函数来更新datagridview

if (this.OpenConnection() == true)
    {
        MySqlCommand sqlCmd = new MySqlCommand("sp_clientgridview", connection);
        sqlCmd.CommandType = CommandType.StoredProcedure;

        mySqlDataAdapter = new MySqlDataAdapter(sqlCmd);
        DataSet DS = new DataSet();
        mySqlDataAdapter.Fill(DS);
        sqlCmd.ExecuteNonQuery();
        kryptonDataGridView1.DataSource = DS.Tables[0];
        kryptonDataGridView1.Columns[0].Visible = false;
        kryptonDataGridView1.Columns[2].Visible = false;
        kryptonDataGridView1.Refresh(); //This or below will work
        this.Refresh();
    }

答案 2 :(得分:0)

确定参考Mark的代码我创建了一个LoadClient();功能

  private void LoadClient()
    {
        var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString;

        connection = new MySqlConnection(connectionString);

        if (this.OpenConnection() == true)
        {
            MySqlCommand sqlCmd = new MySqlCommand("sp_clientgridview", connection);
            sqlCmd.CommandType = CommandType.StoredProcedure;

            mySqlDataAdapter = new MySqlDataAdapter(sqlCmd);
            DataSet DS = new DataSet();
            mySqlDataAdapter.Fill(DS);
            sqlCmd.ExecuteNonQuery();
            kryptonDataGridView1.DataSource = DS.Tables[0];
            kryptonDataGridView1.Columns[0].Visible = false;
            kryptonDataGridView1.Columns[2].Visible = false;
        }
    }

并在我的kryptonDataGridView1_CellDoubleClick事件方法中我改变了f2.Show();到f2.ShowDialog();我调用了我的LoadClient();这里代替frmClient_Load_1方法,如下所示。

  private void kryptonDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            frmUpdate f2 = new frmUpdate();


            f2.lblClientID.Text = kryptonDataGridView1.SelectedRows[0].Cells["ClientID"].Value.ToString();
            f2.lblClearinAgentID.Text = kryptonDataGridView1.SelectedRows[0].Cells["Clearing_Agent_ID"].Value.ToString();
            f2.textboxClientCode.Text = kryptonDataGridView1.SelectedRows[0].Cells["Client Code"].Value.ToString();
            f2.txtboxClientName.Text = kryptonDataGridView1.SelectedRows[0].Cells["Client Name"].Value.ToString();
            f2.txtboxPostalAddress.Text = kryptonDataGridView1.SelectedRows[0].Cells["Postal Address"].Value.ToString();
            f2.txtboxTelephone.Text = kryptonDataGridView1.SelectedRows[0].Cells["Telephone"].Value.ToString();
            f2.txtboxFax.Text = kryptonDataGridView1.SelectedRows[0].Cells["Fax"].Value.ToString();
            f2.txtboxEmailAddress1.Text = kryptonDataGridView1.SelectedRows[0].Cells["E-mail Address 1"].Value.ToString();
            f2.txtboxEmailAddress2.Text = kryptonDataGridView1.SelectedRows[0].Cells["E-mail Address 2"].Value.ToString();
            f2.txtboxEmailAddress3.Text = kryptonDataGridView1.SelectedRows[0].Cells["E-mail Address 3"].Value.ToString();
            f2.txtboxWebsite.Text = kryptonDataGridView1.SelectedRows[0].Cells["Website"].Value.ToString();
            f2.txtboxChargeRate.Text = kryptonDataGridView1.SelectedRows[0].Cells["Charge Rate"].Value.ToString();
            f2.txtboxTotalDepo.Text = kryptonDataGridView1.SelectedRows[0].Cells["Total Deposit"].Value.ToString();
            f2.txtboxAccountBal.Text = kryptonDataGridView1.SelectedRows[0].Cells["Account Balance"].Value.ToString();

            f2.ShowDialog();

            LoadClient();
        }

然后在我的Update按钮点击事件中,我明确地调用了kryptonDataGridView1_CellDoubleClick方法

  private void btnUpdate_Click(object sender, EventArgs e)
    {

       kryptonDataGridView1_CellDoubleClick(null, null);

    }