开发者

Refreshing a DataGridView after DB has changed?

开发者 https://www.devze.com 2023-03-12 02:53 出处:网络
I need to \"refresh\" a DataGridView, bound to a database table, on a form within a TabControl The DataGridView is loaded correctly on startup...

I need to "refresh" a DataGridView, bound to a database table, on a form within a TabControl The DataGridView is loaded correctly on startup... But if the data in the DataBase change, How do I开发者_如何学编程 refresh it to reflect new records or updates?

this is what I am doing in code, after looking for some examples on the web:

    MyTabBindingSource.EndEdit()
    Me.MyTableAdapter.ClearBeforeFill = True

    Me.MyTableAdapter.Fill(Me.MyDataSet.MyTable)

    MyDataGridView.Update()
    MyDataGridView.Refresh()

but nothing changes at all...Do I need to refresh/repaint the TabControl as well as the Form containing it? or what else??


Assuming the datagridview is bound to myTable in the dataset, calling the update() may be the problem. Try getting rid of this.

If that doesn't work try rebinding to mytable again and refresh the DGV.

You shouldn't need to refresh the tab or form.


I'm assuming you have edited the data straight from datagridview and updated it into the server.

You can get/view the updated ones by ticking the "Enable Editing" from datagridview's properties.

Datagridview Tasks

Enable Adding
Enable Editing -- check this one
Enable Deleting
Enable Column Reordering

Once you run your code that clears and fills the datagridview, you will have the new ones.


The way I do this is I clear the DataGridView's DataSource then re-bind it again.
Try:

MyDataGridView.DataSource = Nothing
MyDataGridView.Rows.Clear
MyDataGridView.DataSource = MyTable?
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号