简体   繁体   中英

C#, Databound Datagridview showing wrong Data after inserting row programmatically

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.

使用您在第一页加载时使用的相同查询重新查询。

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM