Refresh datagridview or Reload form

ssabc

Well-known member
Joined
Dec 11, 2017
Messages
63
Programming Experience
10+
Hello:

I am using EF6.

I am adding a new record, and simply want it to refresh my datagridview.

Everything works great if I manually close and re-open. I tried some this.refresh stuff.

After reading several posts, my latest attempt is so close the form and show it again, but it never comes back up.

C#:
            this.Hide();
            frmProduction f2 = new frmProduction();
            f2.Show();

I believe this is standard C# stuff, and should not require EF6 to accomplish, right?

Thanks for the usual great help on this forum!
 
Last edited:
Closing the form and displaying a new form is definitely not the way to go. The only reason to close the form is if you want the form closed. If you want the form open and just need some data in the form adjusted, do that.

Presumably you have your DataGridView bound to a list of data objects populated from the database. You have two choices:

1. Update the contents of that list based on the current database contents.
2. Create a new list and bind that in place of the old one.

Neither of those two options is particularly complex, so I would suggest that you choose which you think is better in your situation, make an attempt at it and then post back with the specific details if you encounter an issue.
 
That said, it may well be that you're doing things the wrong way around. Many, MANY beginners do. If you query the database to get list of records that you display in a DataGridView and you want to add a new record, the proper way to do it is to add the item to the list first, so the grid reflects that change immmediately, then save the changes from the list to the database. If you're adding the item to the database first and wondering how to then update the grid to reflect the change, you're doing it wrong.
 
Closing the form and displaying a new form is definitely not the way to go. The only reason to close the form is if you want the form closed. If you want the form open and just need some data in the form adjusted, do that.

Presumably you have your DataGridView bound to a list of data objects populated from the database. You have two choices:

1. Update the contents of that list based on the current database contents.
2. Create a new list and bind that in place of the old one.

Neither of those two options is particularly complex, so I would suggest that you choose which you think is better in your situation, make an attempt at it and then post back with the specific details if you encounter an issue.

The reason why I cannot add data in the proper way is:

C#:
private void AddRow(DataGridView dgv)
        {
            // dgv.Rows.Add(); // This cannot be done when the control is data bound
            // Need to add to the database

I had actually tried this first.

I am having no problems changing data and deleting data. It updates immediately. It's adding data. I have to completely exit my application and reopen to see it, at which point everything looks great!

Please let me know what I should do, and thanks again.
 
Last edited by a moderator:
But that's not the proper way. As you say, you can't add rows to a bound grid. You have to modify the data source. If you are bound to a DataTable via a BindingSource then you can create a new row by calling AddNew and EndEdit on the BindingSource. If you're bound to a List<T> where T is some custom type then you need to create an instance of that type yourself and add it to the List. That won't update the grid by default though. That's why you should either use a BindingList<T> or bind via a BindingSource. The BindingSource has methods that will update the bound control when changes are made to the data source, e.g. ResetBindings.

By the way, if you want to quote a post in your reply then please use the Reply With Quote button on that post for clarity.
 
Back
Top Bottom