0

I have a databound datagridview for a simple application. I am using the built-in visual studio database, but am using SQL queries to manipulate the data as I prefer it. The datagrid view is only databound because it is a requirement.

After executing any the datagridview does not update by itself, however even after much research and testing everything I could find, nothing seems to work. The data only updates after I restart the program.

This is what I currently have:

dgvEmployee.DataSource = null;
dgvEmployee.Rows.Clear();
this.employeeBindingSource.ResetBindings(true);
dgvEmployee.DataSource = this.employeeBindingSource;

I know it is not the fault of my queries since opening the database shows the added values. Thank you for your time. This is my first question on this site so please go easy on me ;)

Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
  • well, did you commit your transaction? ;) – Tymoteusz Paul Nov 12 '14 at 02:32
  • I just make a SQL connection, send in the prebuilt query, execute, and then close the connection. – DannyDaMan Nov 12 '14 at 02:39
  • Righto, i must've missread your question the first time. If you only get fresh data after a reset. Try, for testing sake, to reconnect to the db and fetch data again, see if that will update them. – Tymoteusz Paul Nov 12 '14 at 02:41
  • That could be easy enough, but the data is being fetched by a databound datagridview. Wiping and reseting the datasource as above does nothing. I'll look into disconnecting the databound grid. – DannyDaMan Nov 12 '14 at 03:16

2 Answers2

0

Have you tried refreshing the main form UI right after? Something like this:

dgvEmployee.DataSource = null;
dgvEmployee.DataSource = this.employeeBindingSource;
this.Refresh();
this.Update();
ryancdotnet
  • 2,015
  • 16
  • 33
0

I am using this code and it is working well, but not sure if it is a best practice:

List<MyEmployeeData> lstDataSource = query.ToList();
dgvEmployee.DataSource = new BindingListView<MyEmployeeData>(lstDataSource);
dgvEmployee.Refresh();

It uses BindingListView class available via NuGet.

In your code theese two lines

dgvEmployee.DataSource = null;
dgvEmployee.Rows.Clear();

are in my opinion not necessary.

But you are missing something like this:

this.employeeBindingSource.DataSource = tableWithNewData;
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105