Hi,

I have a c# windows form app that hanldes memberships. When the user click "new member" a second form pops up where the user can enter in the new data using the details view. When complete the user saves and closes this second form, returning to the original form which has a dataGridView.

What I'm struggling with is a way to force the original form to refresh once the new information has been entered. The nearest I have come is to have either a button the user can click which refills the data table or to have the refill tied to a timer based event. both of those arent particularly good.

Can anyone help?

thanks

Dani AI

Generated

Quick summary: extract the code you use on MainForm_Load into a single refresh method (for example RefreshMemberGrid()), then call that method after the add-member form finishes. There are three clean patterns to do that reliably — pick the one that matches how you show the child form.

  • Modal child (simplest). Show the add form with ShowDialog. Have the child set DialogResult = DialogResult.OK when the save completes, then call your refresh routine after ShowDialog returns.
using (var f = new NewMemberForm())
{
    if (f.ShowDialog(this) == DialogResult.OK)
    {
        RefreshMemberGrid(); // call the same routine the Load handler uses
    }
}
  • Event/callback (works if you must show the child modeless). Define a MemberSaved event on the child and raise it after the insert completes. Subscribe on the main form before showing the child so the main form can call RefreshMemberGrid() (or update the BindingSource) when the event fires. If the event may be raised from a background thread, marshal to the UI thread with Invoke/BeginInvoke.
// on main:
var f = new NewMemberForm();
f.MemberSaved += (s,e) => RefreshMemberGrid();
f.Show(this);
  • Share the data objects (minimal requery). Pass the BindingSource or the DataTable/ DataSet instance into the child so it can add the new row directly. After the child closes, call bindingSource.ResetBindings(false) or bindingSource.EndEdit() so the grid updates without re-fetching everything.

Notes and cautions:

  • Avoid timers for this. They mask the problem.
  • If you requery the whole table for every new member, consider fetching only the inserted row (by key) or adding the new row to the existing DataTable to improve performance.
  • Don’t call AcceptChanges() before calling your TableAdapter/adapter Update() — that will prevent row-state tracking and break updates.

Both the modal-ShowDialog pattern and the event approach are common and safe; ’s delegate idea is along the same lines, and extracting the refresh into one method keeps the code tidy for .

Recommended Answers

All 2 Replies

How do you populate the users list on main form? Is it from a DataTable (or dataSet) or some array list?
You can use a delegate to pass the data from one form to another, and refreshing the gdv control with calling the method to populate it.
YOu can take a look into this simple example, wgich shows how to pass data from one form to another (from textBox to a label) - you can do the same, only to pass the data to some other method, which would populate dgv (or add a new row to it):

//FORM1:
    public partial class Form1 : Form
    {
        delegate void MyDelegate(string str);

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this);
            form2.Show(this);
        }

        public void PassingParameters(string item)
        {
            if (label1.InvokeRequired)
            {
                MyDelegate d = new MyDelegate(PassingParameters);
                label1.Invoke(d, new object[] { item });
            }
            else
                label1.Text = item;
        }
    }

//FORM2:
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 _form1)
        {
            InitializeComponent();
            form1 = _form1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string item = textBox1.Text;
            form1.PassingParameters(item);
        }
    }

Hi thanks for your post!

yes its populated via a data table/set. something like...

this.membersTableAdapter.Fill(this.dsMemberWithNotes.Members);

this currently sits within the MainForm_Load method

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.