1

I´m having a problem with a databound DataGridView:

I´m using a view including inner joins from an SQL-Server Database as Datasource. When I insert rows programmatically the Data is written correctly, but the DataGridView is not displaying the data it´s supposed to display.

Some of the Columns in the View are joined with another table. Basically I join ID-values in the main table with sub tables to be able to display the text value of the column instead. When I insert the rows containing the id values into the GridView, it displays the ID-value, but not the corresponding text value.

If I restart the program everything is displayed correctly. How do I update/refresh the GridView to show the correct text values? I´ve tried GridView.EndEdit() and GridView.Refresh(), rebindung the Datasource to the GridView and also have been searching the web without success.

Chris
  • 835
  • 12
  • 27
  • Is it similar to this issue: http://stackoverflow.com/questions/3340192/datagridview-bound-to-bindinglist-does-not-refresh-when-value-changed – SamFisher83 Apr 04 '13 at 15:43
  • Did you try yourGridView.Refresh()? – Doan Cuong Apr 04 '13 at 15:46
  • may be this link will helps you http://stackoverflow.com/questions/11020384/how-do-i-update-reload-datagridview-bindingsource – Glory Raj Apr 04 '13 at 16:11
  • @DoanCuong Yes I have, see the part of my question: "I´ve tried GridView.EndEdit() and GridView.Refresh()" ;-) – Chris Apr 05 '13 at 12:16
  • @pratapk Thanks, but that isn´t my issue. I am getting an updated Gridview, but it´s showing the wrong values - meaning it´s showing the id values, not the appropiate text values. – Chris Apr 05 '13 at 12:19
  • @SamFisher83 Thanks, I will try that out. I had already seen that question, but will have a closer look now at notifications. – Chris Apr 05 '13 at 12:21

2 Answers2

0

Re-query using the same query you used at the first page load.

Oscar
  • 13,594
  • 8
  • 47
  • 75
  • Hmm, if I get you right, thats what I´m trying to avoid as it would mean reloading thousands of rows and cause a delay... – Chris Apr 04 '13 at 16:02
  • You will have to anyway.. What about if some other user modified some data in the meantime? By refreshing the whole dataset you get the most up to date data, avoiding conflicts. You can always use data-paging to load a subset of data instead of "thousands of rows". – Oscar Apr 05 '13 at 07:44
  • No, I don´t have to, really: If data is changed in the database externally, changing data in the program will result in an DBConcurrencyException, which can be caught in a try catch clause asking the user if he wants to update the data. There are only 15 people working with the database data at the same time. As the data is considerable, it´s not very likely that the Exception is thrown. So I´m trying to avoid reloads as much as possible. Unfortunately paging is not an option :-( – Chris Apr 05 '13 at 12:09
0

DataGridView.Refresh() is for a graphical refresh, not a data refresh. You need to rebind your data source after the update.

Set the data source to null after the insert: DataGridView.DataSource = null; and then rebind the data source by re-querying the data and applying the updated source DataGridView.DataSource = someSource, and finally call DataBind() once again.

It it's not performant enough for you, I might suggest limiting the number of rows returned in your result set, or applying some worthy indexes to your table(s).

McCee
  • 1,549
  • 10
  • 19
  • hi! Thanks for the answer. I´ve already answered a similar suggestion by Oscar below: If data is changed in the database externally, changing data in the program will result in an DBConcurrencyException, which can be caught in a try catch clause asking the user if he wants to update the data. There are only 15 people working with the database data at the same time. As the data is considerable, it´s not very likely that the Exception is thrown. So I´m trying to avoid reloads as much as possible. – Chris Apr 05 '13 at 12:14
  • Isn´t it possible to refresh an element (e.g. binding source, data adapter, table) to have the Gridview show the correct values again - without having to reload the data? Thanks in advance for the answer :-) – Chris Apr 05 '13 at 12:14