Marek
Marek

Reputation: 3575

Refresh datagridview records from another form

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.

Upvotes: 1

Views: 3340

Answers (2)

Mojtaba
Mojtaba

Reputation: 1620

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

Upvotes: 0

Ali Suleyman TOPUZ
Ali Suleyman TOPUZ

Reputation: 36

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..

Upvotes: 2

Related Questions