Refresh datagridview records from another form

纵饮孤独 提交于 2020-01-05 11:52:45

问题


I have dataGridView1 in form1 but for inserting in it I'm using form2 which have save button (that provides insert into datagridview1) to which I'm trying to set - refresh or select * from so I dont need any refresh button in form1 whenever I want to show up just all the recors. Is there any way to achieve that?

Thanks much for your time.


回答1:


You can use delegate.. For example :

Form1 :

private void btnForm1_Click(object sender, System.EventArgs e)
{

// Create an instance of form 2
    Form2 form2 = new Form2();

    // Create an instance of the delegate
    form2.passControl = new Form2.PassControl(PassData);

    // Show form 2
    form2.Show();
}

private void PassData(object sender)
{
    // Set de text of the textbox to the value of the textbox of form 2
    txtForm1.Text = ((TextBox)sender).Text;
}

Form 2 :

public class Form2 : System.Windows.Forms.Form
{
    // Define delegate
    public delegate void PassControl(object sender);

    // Create instance (null)
    public PassControl passControl;

    private void btnForm2_Click(object sender, System.EventArgs e)
    {
        if (passControl != null)
        {
            passControl(txtForm2);
        {
        this.Hide();
    }
}

I hope this helps..




回答2:


You can use Form.DialogResult to achieve this.

In your insert form, after saving data to database:

DbContext.SaveChanges();
MessageBox.Show("Successfully saved.");
DialogResult = System.Windows.Forms.DialogResult.Yes; 

and In your grid form:

Frm_NewData newData = new Frm_NewData();
if (newData.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
{
     RefreshGrid();
}


来源:https://stackoverflow.com/questions/17478889/refresh-datagridview-records-from-another-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!