none
refreshing form RRS feed

  • Question

  • I have updated data in a table then displayed the data in a DGV on the form but the displayed data doesn't change.  I have tried to refresh the form using this.Refresh(); and in combination this.SuspendLayout(); this.ResumeLayout(); at the end of the display method (inserts data into DGV). Neither of which works.  So I am left with restarting the application to display the updated data.  Has anyone found a solution to this?
    Saturday, February 3, 2018 2:43 PM

Answers

  • Got it working.  Pass instance using in a button on form1

    Form2 form2 = new Form2(this);
    form2.Show();

    In Form2

    Private Form1 NewForm1 = null;
    
    public Form2(Form form1)
    {
    InitializeComponent();
    NewForm1 = form1 as Form1
    }
    

    After updating tables call method to display(Load) in DGV.

    NewForm1.LoadForm();
    this.Close();

    Friday, February 16, 2018 11:36 PM

All replies

  • How do you display or assign the data to Data Grid View? Give some details.

    If you mean that there is a DataTable which is assigned to dataGridView.DataSource in a Form application, then changing the data should be reflected automatically.

    Saturday, February 3, 2018 4:35 PM
  • I am displaying data from multiple tables (sql server 2016) in a DGV via class objects. When the user clicks update, another windows form opens from which the users chooses which data to update.  On clicking update from that window, current data is selected from other tables, again via class objects.  The appropriate DGV cells are then updated using these class objects.  Everything is working, except when I assign the updated values, they don't show automatically as I had hoped/expected.  I have to restart the application to see the updated values.  
    Saturday, February 3, 2018 5:09 PM
  • If the technique is based on assigning class objects to DataSource, then try assigning null, then re-assign your class objects.


    • Edited by Viorel_MVP Saturday, February 3, 2018 6:01 PM
    Saturday, February 3, 2018 6:00 PM
  • A DGV with 6 columns.  A single method populates all columns from a main database sql server.   The DGV has one row of data. To update, each column is updated from different database.  Data is selected from these databases which updates the main database tables.  To update the DGV, I'm copying the code that populates the DGV initially, but into separate methods, one for each column.  The problem is, although I have referenced the columns first, in doing a comparison between a cell value and an object property, the cell value returns null.  I think I creating new columns instead of referencing existing columns, that would explain null values.  If I don't create/reference the columns first, I get unknown column exception.  How do I reference existing DGV columns from a separate method?
    Sunday, February 4, 2018 1:40 AM
  • Before dealing with columns, make sure that you do not accidentally create a different form (using ‘new’) or a different data grid. You should work with original objects.

    Sunday, February 4, 2018 8:15 AM
  • Got it working.  DGV now displaying updated value(s) without restarting application.  My code to update the DGV wasn't wrong, it just wasn't being executed, flow control (logic) was wrong.
    Sunday, February 4, 2018 9:40 AM
  • This is my structure

    public Class1{//main form
    
    InitializeComponent();
    
    }
    
    private void class1_Load(){ //creates DGV's}
    
    public void method1(){ //populates DGV's 
    
    Class3.method1();
    
    //populate column1
    
    Class3.method2();
    
    //populate column2
    
    Class3.method3();
    
    //populate column3
    
    Class3.method4();
    
    //populate column4
    
    Class3.method5();
    
    //populate column5
    
    Class3.method6();
    
    //populate column6
    
    }
    
    public static void method2(){// updated values in DGV}
    
    private void method3(){/button
    
    Form form2 = new Form();
    
    form2.visible.true;
    
    }
    
    }
    
    Class2{//form2
    
    if(check1.checked)
    
    {Class3.method7}
    
    Class1.method2();
    
    this.close()
    
    }
    
    Class3{
    
    public static void method1(){ create object from sql select}
    
    public static void method2(){create object from sql select}
    
    public static void method3(){create object from sql select}
    
    public static void method4(){create object from sql select}
    
    public static void method5(){create object from sql select}
    
    public static void method6(){create object from sql select}
    
    public static void method7(){create object from sql select}
    
    }


    The problem is Class1 method2, referencing the DGV to update it.  If I create a class object, the DGV cell reference returns a null reference exception.


    Sunday, February 4, 2018 12:17 PM
  • How do I access a DGV from directly or indirectly from another class?  The update method is currently non-static.  If I call the method using a class instance, this.DataGridView.Rows.Count is 0.  
    Monday, February 5, 2018 6:01 AM
  • Hi RichardDunneBSc,

    >>The problem is Class1 method2, referencing the DGV to update it.  If I create a class object, the DGV cell reference returns a null reference exception.

    We could achieve it via delegate. like this:

    #Class1 form

    using System;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp
    {
        public partial class Class1 : Form
        {
            public Class1()
            {
                InitializeComponent();
            }
    
            private void Class1_Load(object sender, EventArgs e)
            {
                using (var db = new EFDemoContext())
                {
                    var result = db.CustomTrees.ToList();
                    dataGridView1.DataSource = result;
                }
            }
    
    
            public void method2()
            {
                foreach (DataGridViewRow gRow in dataGridView1.Rows)
                {
                    if (gRow.Cells[1].Value == null)
                    {
                        gRow.Cells[1].Value = "KKK";
                    }
                }
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                var form2 = new Class2();
                form2.UpdateEventHandler += method2;
                form2.Show();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                method2();
            }
        }
    }
    

    #Class2 form

    using System;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp
    {
        public partial class Class2 : Form
        {
            public Class2()
            {
                InitializeComponent();
            }
    
            public delegate void UpdateDelegate();
            public event UpdateDelegate UpdateEventHandler;
    
            private void check1_CheckedChanged(object sender, EventArgs e)
            {
                if (check1.Checked)
                {
                    UpdateEventHandler?.Invoke();
                    this.Close();
                }
            }
        }
    }
    

    Best regards,

    Zhanglong


    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, February 5, 2018 6:23 AM
    Moderator
  • Zhanglong Wu your demo is a bit confusing.  I'm calling method2 from form2, not a second button on form1.  
    Monday, February 5, 2018 9:36 AM
  • Hi RichardDunneBSc,

    >>Zhanglong Wu your demo is a bit confusing.  I'm calling method2 from form2, not a second button on form1.  

    Please ignore the second button, only a test, you could delete it.

    Best regards,

    Zhanglong


    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, February 5, 2018 9:54 AM
    Moderator
  • The DGV is accessible in a non-static method using this, there is a single row in the DGV, but 

    this.DataGridView.Rows.Count
    is 0.  No idea why?

    Monday, February 5, 2018 10:10 AM
  • Hi RichardDunneBSc,

    >>The DGV is accessible in a non-static method using this, there is a single row in the DGV, but is 0.  No idea why?

    Based on your description, I am not what cause the issue, if possible, could you please share a simple demo via OneDrive, which could reproduce the issue, it will be beneficial to resolve the issue.

    Note: please remove privacy information before you upload the demo.

    Best regards,

    Zhanglong


    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.

    Tuesday, February 6, 2018 1:11 AM
    Moderator
  • I don't have the facility to do that.  Here is an overview that I hope better explains the problem. 

    If method2 is static, I call method2 from Class2 via its class.  I have to create an instance of Class1 in method2 to access the DGV,

    Class1 class1 = new Class1(); 

    however, class1.DataGridViewName.Rows.Count throws a NullReferenceException

    If method2 is non-static I have to create an instance of Class1 in Class2 to access method2. Then in method2 this.DataGridViewName.Rows.Count returns 0.

    Tuesday, February 6, 2018 7:50 AM
  • Hi RichardDunneBSc,

    >>If method2 is non-static I have to create an instance of Class1 in Class2 to access method2. Then in method2 this.DataGridViewName.Rows.Count returns 0.

    Do you execute the method via delegate? Based on my test, it could return the count.

    Best regards,

    Zhanglong


    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.


    Tuesday, February 6, 2018 9:15 AM
    Moderator
  • No, I haven't used delegate, not familiar with.  Need to do some reading on it to see how it works.
    Tuesday, February 6, 2018 10:11 AM
  • I see from tutorialspoint.com how delegate can reference a method, I'm trying to reference a Class, not a method.  
    Tuesday, February 6, 2018 11:02 AM
  • Hi RichardDunneBSc,

    >>No, I haven't used delegate, not familiar with.  Need to do some reading on it to see how it works.

    If you do winform development, I would suggest that you could learning about delegate, which is useful for winform development. for more information about delegate, please refer to:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/using-delegates

    Best regards,

    Zhanglong


    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, February 7, 2018 1:29 AM
    Moderator
  • Looking at the docs on delegate, I don't see how it solves my problem.  Delegates are used to pass methods as arguments to other methods.  I'm trying to access a DGV from a separate method to the one that initially added columns and populated it, namely method1().  If I were to call method1() instead, it would create new columns, repeating existing columns. 

    Wednesday, February 7, 2018 11:57 AM
  • Hi RichardDunneBSc,

    I would suggest that you could try the code that I have provided, execute the method via delegate and event.

    Best regards,

    Zhanglong


    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, February 8, 2018 5:30 AM
    Moderator
  • Executing method1 again will recreate existing columns.  If I could separate the creation on the DGV and adding columns from inserting values, that would be helpful.  If that's possible?
    Thursday, February 8, 2018 11:16 AM
  • Got it working.  Pass instance using in a button on form1

    Form2 form2 = new Form2(this);
    form2.Show();

    In Form2

    Private Form1 NewForm1 = null;
    
    public Form2(Form form1)
    {
    InitializeComponent();
    NewForm1 = form1 as Form1
    }
    

    After updating tables call method to display(Load) in DGV.

    NewForm1.LoadForm();
    this.Close();

    Friday, February 16, 2018 11:36 PM