数据集更新 vb.net 后 DataGridView 不刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10133946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:03:30  来源:igfitidea点击:

DataGridView does not refresh after dataset update vb.net

vb.netdatagridviewdatasettableadapter

提问by pec

I have a vb.net form with a dataGridView

我有一个带有 dataGridView 的 vb.net 表单

The dataGridView data source is the dgvTableAdapter with this sql statement

dataGridView 数据源是带有这个sql语句的dgvTableAdapter

SELECT membres.ID, membres.refere_par, bands.titre, 
       membres_1.prenom & ' ' & membres_1.nom  AS reference_nom
FROM ((bands INNER JOIN membres ON bands.ID = membres.[band]) 
      INNER JOIN membres membres_1 ON membres.refere_par = membres_1.ID)

I delete membres from the membres Table like this

我像这样从成员表中删除成员

' Get member id 
Dim userId As Integer 
userId = DataGridView1.Item( 0,0).Value

' Delete the member
Me.MeoshowDataSet2.membres.FindByID(userId).Delete()
Me.MembresTableAdapter.Update(Me.MeoshowDataSet2)

' Refresh datagrid
dataGridView1.Refresh() ' does nothing

I know the delete statement works because I saw the changes in the database. If I close the form and reopen it, the dataGridView is up to date.

我知道删除语句有效,因为我看到了数据库中的更改。如果我关闭表单并重新打开它,则 dataGridView 是最新的。

The membres table is an access table

成员表是访问表

I'm running the app in visual 2010 debug mode.

我正在 Visual 2010 调试模式下运行该应用程序。

采纳答案by David Hall

The usual way of doing this is to reset the DataSourceof the DataGridView.

这样做的通常的方法是重新设置DataSourceDataGridView

Try like this code (with correct code to provide the right table from the dataset):

试试这个代码(使用正确的代码从数据集中提供正确的表):

dataGridView1.DataSource = typeof(List); 
dataGridView1.DataSource = dataset.Tables["your table"];

Calling .Refresh()doesn't work since it only forces a repaint, but the code that paints the grid doesn't know of the changes.

调用.Refresh()不起作用,因为它只强制重绘,但绘制网格的代码不知道更改。

回答by Tim

I stumbled upon this while searching for exact same problem. Didn't find it online though. Here's what worked for me:

我在寻找完全相同的问题时偶然发现了这一点。不过网上没找到。以下是对我有用的内容:

Public Sub RefreshData()
    dTable.Clear()
    dAdapter.Fill(dTable)
    dtaDataGrid.DataSource = dTable
End Sub

Private Sub btnRefresh_Click(sender As System.Object, e As System.EventArgs) Handles btnRefresh.Click
    RefreshData()

    ClearAllTextBox(Me)
End Sub

Cleared all the data in the data table first then refilled it with the data from the data adapter since you said that the database was updated with your code, only that it didn't refresh.

首先清除数据表中的所有数据,然后用数据适配器中的数据重新填充它,因为您说数据库已用您的代码更新,只是它没有刷新。

回答by AlexVMM

You can also use this:

你也可以使用这个:

DirectCast(dataGridView1.DataSource, DataTable).AcceptChanges()

Just replace dataGridView1. The 2nd parameter is the DataTableclass.

只需更换dataGridView1. 第二个参数是DataTable类。

回答by Kristian

...and an alternative that worked for me:

...以及对我有用的替代方案:

'reset datasource

dgvBHL.DataSource = nothing

' Assign datatable to dgv this always works 1st time it is called

dgvBHL.DataSource = dtData 

'To fix the DGV not refreshing properly after the 1st time, switch the sort order

dgvBHL.Sort(dgvBHL.Columns(0), System.ComponentModel.ListSortDirection.Descending)

dgvBHL.Sort(dgvBHL.Columns(0), System.ComponentModel.ListSortDirection.Ascending)

'No need to do a refresh or anything else