如何在子窗体关闭时刷新父窗体上的数据网格视图

本文关键字:窗体 数据 数据网 视图 网格 刷新 | 更新日期: 2023-09-27 18:36:55

我想知道如何在 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;

        }

    }

如何在子窗体关闭时刷新父窗体上的数据网格视图

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

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

这应该可以:)祝您编码愉快!

也许您忘记调用刷新函数来更新数据网格视图

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();
    }
好的,

作为对马克代码的引用,我创建了一个 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。显示();到 F2。显示对话框();我调用了我的加载客户端();在这里而不是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();
        }

然后在我的更新按钮单击事件中,我显式调用kryptonDataGridView1_CellDoubleClick方法

  private void btnUpdate_Click(object sender, EventArgs e)
    {
       kryptonDataGridView1_CellDoubleClick(null, null);
    }