简体   繁体   English

如何刷新按钮上的下拉菜单单击其他表格

[英]How to Refresh dropdown on button click on another form

I have a winform say winForm1 which has combobox. 我有一个winform说winForm1有组合框。 i am binding this combobox on Form_Load event like this 我像这样在Form_Load事件上绑定此组合框

SqlCommand cmd = new SqlCommand("SELECT DISTINCT(TXT_TARGET_NUMBER) FROM TBL_CDR_ANALYZER", sqlCon);
cboTargetNo.Properties.Items.Clear();
cboTargetNo.Properties.Items.Add("Choose Target Number");
sqlCon.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
    cboTargetNo.Properties.Items.Add(dr["TXT_TARGET_NUMBER"]);
}
sqlCon.Close();
cboTargetNo.SelectedIndex = 0;

Now this form also contains a button say btn1. 现在,此表单还包含一个名为btn1的按钮。 on Click event of this button i am opening a new winform say winForm2 using ShowDialog() function. 在此按钮的Click事件上,我正在使用ShowDialog()函数打开一个新的winform,说winForm2。

On WinForm2 i have a button btn2 which insert some values in sql. 在WinForm2上,我有一个按钮btn2,该按钮在sql中插入一些值。 i want after values insert in sql, Combobox on winForm1 should Refresh. 我想将值插入sql后,winForm1上的组合框应刷新。 How can i do this. 我怎样才能做到这一点。 i am clueless where to start for this. 我对此一无所知。

If you want to update each form1 from form2 better option is having event handler. 如果要从form2更新每个form1 ,更好的选择是使用事件处理程序。

Form2 Code Form2代码

public event EventHandler myEvent;
public Form2()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    // update data here 

    // inform form 2 about data added 
    if (myEvent != null)
    {
        myEvent(this,e);
    }
}

Form1 Code Form1代码

public Form1()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.myEvent += new EventHandler(f2_myEvent);
    f2.ShowDialog();
}

void f2_myEvent(object sender, EventArgs e)
{
    //Refresh your ddl
}

Simplist way would be to to extract the code for ComboBox update to a method, Call it from Form_Load event , Later since you are using Form.ShowDialog , you can call the method again to get the latest record from the database and bind the CombobBox again. 简化列表的方法是将ComboBox更新的代码提取到方法中,从Form_Load event调用它,稍后,由于您使用Form.ShowDialog ,因此可以再次调用该方法以从数据库中获取最新记录,并再次绑定CombobBox 。 Something like: 就像是:

WinForm2 frm2 = new WinForm2();
frm2.ShowDialog();
RefreshCombo();

Where RefreshCombo is a method : RefreshCombo是一种方法:

private void RefreshCombo()
{
 SqlCommand cmd = new SqlCommand("SELECT DISTINCT(TXT_TARGET_NUMBER) FROM TBL_CDR_ANALYZER", sqlCon);
cboTargetNo.Properties.Items.Clear();
cboTargetNo.Properties.Items.Add("Choose Target Number");
sqlCon.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
    cboTargetNo.Properties.Items.Add(dr["TXT_TARGET_NUMBER"]);
}
sqlCon.Close();
cboTargetNo.SelectedIndex = 0;
}

If you want immediate update, you could create a delegate to refresh the Combo from your dialog after hitting the button. 如果要立即更新,则可以创建一个委托,以在单击按钮后刷新对话框中的“组合”。

Create a delegate in your main form: 以您的主要形式创建一个委托:

public delegate void ComboDelegate();//namespace level

public void RefreshCombo(string itemToAdd){
   //do your add item here
}

then in your Dialog Form class: 然后在您的Dialog Form类中:

public ComboDelegate cd;

Create your Dialog Form with the cd set to RefreshCombo: 将cd设置为RefreshCombo创建对话框形式:

winForm2 = new Winform(){ cd = RefreshCombo );

then at your button Click simply call: 然后单击您的按钮,只需调用:

cd(itemToUpdate);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM