Refresh DataGridView after data inserted in DB by another dialog

南楼画角 提交于 2020-01-06 04:30:06

问题


On my Winforms app, I have a primary form with a DataGridView bound to a database Entity datasource.

The grid is set up not to allow inserts. Instead I have a button on my form that kicks of a second dialog where the insert takes place (ie. with friendlier ui than is possible with the DataGridView).

The insert is working fine.. query of the underlying table in db shows that the record has been inserted. However, I can't seem to get the DataGridView on the primary form to see the new data just created by the second dialog.

I have read many Stack Overflow q & a's and tried various solutions to get the DataGridView to refresh to show new data.. but nothing works.

This must be a common situation ?? Can someone suggest some VB.NET code that will work ?

Thanks,

Bazza

    Dim qry = From o In mDB.tblFOMTestResults From p In mDB.tblProduct.DefaultIfEmpty _
                                              From c In mDB.tblCalibration _
                Where o.ProductID = p.ID And o.CalibrationID = c.ID _
                Order By o.RunDate, p.ProductName Select _
                o.ID, o.SampleCode, o.RunDate, o.ConditioningDays, o.ConditioningRHAndTemp, _
                o.TestArea, o.EdgeSeal, o.SealedBags, o.FaceSeal, _
                o.MoisturePercentage, o.CalibrationID, o.FYFOMH1, o.FYFOMH2, o.FYFOMH3, _
                o.FYFOMH4, p.ProductName, o.ProductID

    Me.TblFOMTestResultsDataGridView.DataSource = qry

回答1:


Try to bind to the results of the query rather than the query:

dataGridView.Source = query.Execute(MergeOption.AppendOnly);



回答2:


After trying a number of things, I finally got this going by following the advice from the following SO answer.. Refresh DataGridView in Windows form



来源:https://stackoverflow.com/questions/11075031/refresh-datagridview-after-data-inserted-in-db-by-another-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!