none
Refreshing form to display updated data RRS feed

  • Question

  • When I Run my program Form1 opens and displays data in DataViewGrids from various DB's.  To update any information, clicking Update button opens Form2 which gives the user options, which information to update.  After selecting which information to update, code executes to update the information in one or more DB's.  This works.  I want to refresh Form1 so that it selects and displays the updated data, essentially re-running the program, then close Form2.   What is the best way of doing this?  Does refreshing Form1 run the code in Form1?  So far, the only way of displaying the updated data is to manually close the program and re-run it.
    Thursday, December 27, 2018 11:26 PM

All replies

  • Hi Richard,

    Welcome to the MSDN forum.

    Refer to the description, this issue is about the Windows Forms development and since our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum for a better support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Friday, December 28, 2018 1:31 AM
  • Hi,

    >> Refreshing form to display updated data

    To achieve this need, you can use "Delegate" and "Event". What you need to do is subscribing the event when you create a "Form2". Here is the demo you can refer to.

    Code in Form1:

        private void Update_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            // subscribe event
            f2.UpdateDelHandler += Refreshdata;
            f2.ShowDialog();
        }
    
        string connStr = @"connection string";
    
        // the method refresh datagridview
        private void Refreshdata()
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlDataAdapter sda = new SqlDataAdapter("Select * From TestData", conn);
                DataSet Ds = new DataSet();
                sda.Fill(Ds, "T_D");
                this.dataGridView1.DataSource = Ds;
                this.dataGridView1.DataMember = "T_D";
            }
        }
    
        // load initial data
        private void Form1_Load(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                // load date
                SqlDataAdapter sda = new SqlDataAdapter("Select * From TestData", conn);
                DataSet Ds = new DataSet();
                sda.Fill(Ds, "T_D");
                this.dataGridView1.DataSource = Ds;
                this.dataGridView1.DataMember = "T_D";
            }
        }
    

    Code in Form2:

        // declare delegate and event
        public delegate void UpdateDel();
        public event UpdateDel UpdateDelHandler;
    
        string connStr = @"connection string";
    
        private void UpdateData_Click(object sender, EventArgs e)
        {
            // update database
            string sqlUpdate = $"update TestData set Score=@score where Id = @id";
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                SqlCommand sqlcomUp = new SqlCommand(sqlUpdate, conn);
                SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@score", SqlDbType.Int, 50),
                new SqlParameter("@id", SqlDbType.Int, 50),
                };
                paras[0].Value = Convert.ToInt32(tbScore.Text);
                paras[1].Value = Convert.ToInt32(tbID.Text);
                foreach (var item in paras)
                {
                    sqlcomUp.Parameters.Add(item);
                }
                conn.Open();
                sqlcomUp.ExecuteNonQuery();
            }
            // trigger event
            UpdateDelHandler();
        }

    Result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 28, 2018 7:22 AM
    Moderator
  • Thanks Kyle.  What would have been useful would be to close Form1 after opening Form2, but that seems not possible.  I'm not passing any data between forms, just closing Form2 and refreshing Form1.   Does delegate do that?  
    Friday, December 28, 2018 4:56 PM
  • Hi,

    >> Does delegate do that?

    Yes, you can do it by delegate too. You can trigger the delegate in event "FormClosing".

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            // trigger event
            UpdateDelHandler();
        }
    

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 31, 2018 1:17 AM
    Moderator
  • I think I have figured out whats happening.  I have 

    Form1 form1 = new Form1();
    in Form2, is it referring to the existing instance of Form1 or creating a new instance?  If the latter, how do I refer to the existing instance?  

     
    Monday, December 31, 2018 12:24 PM
  • Hi,

    >> is it referring to the existing instance of Form1 or creating a new instance?

    The code you mentioned above will create a new instance.

    >> If the latter, how do I refer to the existing instance? 

    Could you tell me where did you create "the existing instance"? It would be better if you can provide the related code.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 2, 2019 2:17 AM
    Moderator
  • the code for Form1 is

    namespace ProjectName
    {
        public partial class FormName : Form
        {
    
            public FormName()
            {
                InitializeComponent();
            }
    
            private void FormName_Load(object sender, EventArgs e)
            {
                functionName();
            }
    
            public void functionName()
            {
    
            }
        }
    }

    Basically I want to refresh Form1 from Form2, without having to reload Form1 (close and open again).

    Wednesday, January 2, 2019 11:04 AM
  • Hi,

    >> Basically I want to refresh Form1 from Form2, without having to reload Form1 (close and open again).

    The demo I provided before is to implement this function. In this demo, Form1 did not close and open again. It only refreshed the data in Form1 via "delegate". Do I have any misunderstanding about your need? If so, please let me know.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 3, 2019 1:37 AM
    Moderator
  • The only part of the code that looks odd is

    f2.UpdateDelHandler += Refreshdata;

    I was expecting

    f2.UpdateDelHandler() += Refreshdata();

    What does += mean in this context?

    Thursday, January 3, 2019 6:03 AM
  • Hi,

    >> What does += mean in this context?

    Via "+=", you can add an extra method to the delegate's list of methods. More details about "Delegate", you can refer to Using Delegates.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 3, 2019 7:12 AM
    Moderator