【问题标题】:how to refresh datagridview on parent form when child form closes子窗体关闭时如何刷新父窗体上的datagridview
【发布时间】:2015-03-07 05:28:34
【问题描述】:

我想知道如何在 .showDialog 关闭时打开 frmUpdate 时自动刷新我在 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;



        }


    }

【问题讨论】:

  • 你能不能不清空父数据源然后重新分配它..?
  • 如何将数据绑定到当前的datagridview...?也许显示更多代码我将能够更好地协助..我也在谈论绑定这很容易
  • 我已经更新了代码
  • 相信这篇文章有你想要的答案:stackoverflow.com/questions/2395624/…

标签: c#


【解决方案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();
}

应该这样做 :) 编码愉快!

【讨论】:

  • 我在 LoadClient() 上得到带下划线的错误;在 frmUpdate 中,它说它在上下文中不存在,而且我不想重新加载 frmClient 但在我调用 frmUpdate this.Close(); 时刷新数据网格;
  • 所有这些代码都只在frmClient中..这不会重新加载frmClient而是刷新datagrid视图中的数据。
  • 好吧,我不会直接从 .ShowDialog() 打开 frmUpdate;当它的按钮被点击时,我宁愿从按钮更新点击中调用`kryptonDataGridView1_CellDoubleClick`,就像这样 kryptonDataGridView1_CellDoubleClick(null, null);在 frmUpdate 的文本框中使用选定的数据网格行值打开 frmUpadate,我想你也应该知道这一点
  • private void kryptonDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { frmUpdate frm = new frmUpdate(); frm.ShowDialog() frm.Textbox1.text = kryptonDataGridView1.SelectedRows[0].Cells[0].Value.ToString(); //通过再次调用LoadClient()刷新datagridview;加载客户端(); }
  • tanx 它终于奏效了,我使用您的流程作为参考并在我的流程中进行了修改,它完全按照我的意图为您做+10。以后我会把我的整个过程作为参考发布给其他人
【解决方案2】:

可能你忘了调用刷新函数来更新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();
    }

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-01-24
      • 1970-01-01
      • 2012-07-14
      • 2011-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 1970-01-01
      相关资源
      最近更新 更多