每次我按下刷新按钮时 DataGridView 都会复制 vb.net
DataGridView duplicating everytime I press refresh button vb.net
我正在尝试刷新我的 DataGridView1。我想我必须添加一个清晰的功能,因为每次我按下刷新按钮时它都会复制数据。我没有为 DataGridView1 使用数据源。
提前感谢您的宝贵时间。
编码:
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
Refreshdata()
End Sub
Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\ahmed\OneDrive\Desktop\ProjectDatabase2003.mdb")
Dim DS As DataSet = New DataSet
Dim DA As OleDbDataAdapter
Dim tables As DataTableCollection = DS.Tables
Dim source1 As New BindingSource()
Private Sub Refreshdata()
DA = New OleDbDataAdapter("Select * from Risk_Register", myConnection)
DA.Fill(DS, "Risk_Register")
Dim view1 As New DataView(tables(0))
source1.DataSource = view1
DataGridView1.DataSource = view1
DataGridView1.Refresh()
End Sub
Fill
方法可以复制行,
The Fill operation adds the rows to the specified destination
DataTable object in the DataSet, creating the DataTable object if it
does not already exist. When you create a DataTable object, the Fill
operation ordinarily creates only column name metadata. However, if
the MissingSchemaAction property is set to AddWithKey, appropriate
primary keys and constraints are also created.
If primary key information is present, any duplicate rows are
reconciled and only appear one time in the DataTable that corresponds
to the DataSet. Primary key information may be set either through
FillSchema, by specifying the PrimaryKey property of the DataTable, or
by setting the MissingSchemaAction property to AddWithKey.
确保在目标 table 上定义了 PK。或者在填充之前清除 table,但要处理 table 参与的 FK 和关系(如果有的话)。
我认为你应该调用适配器下可用的更新方法。
我正在尝试刷新我的 DataGridView1。我想我必须添加一个清晰的功能,因为每次我按下刷新按钮时它都会复制数据。我没有为 DataGridView1 使用数据源。
提前感谢您的宝贵时间。
编码:
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
Refreshdata()
End Sub
Dim myConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\ahmed\OneDrive\Desktop\ProjectDatabase2003.mdb")
Dim DS As DataSet = New DataSet
Dim DA As OleDbDataAdapter
Dim tables As DataTableCollection = DS.Tables
Dim source1 As New BindingSource()
Private Sub Refreshdata()
DA = New OleDbDataAdapter("Select * from Risk_Register", myConnection)
DA.Fill(DS, "Risk_Register")
Dim view1 As New DataView(tables(0))
source1.DataSource = view1
DataGridView1.DataSource = view1
DataGridView1.Refresh()
End Sub
Fill
方法可以复制行,
The Fill operation adds the rows to the specified destination DataTable object in the DataSet, creating the DataTable object if it does not already exist. When you create a DataTable object, the Fill operation ordinarily creates only column name metadata. However, if the MissingSchemaAction property is set to AddWithKey, appropriate primary keys and constraints are also created.
If primary key information is present, any duplicate rows are reconciled and only appear one time in the DataTable that corresponds to the DataSet. Primary key information may be set either through FillSchema, by specifying the PrimaryKey property of the DataTable, or by setting the MissingSchemaAction property to AddWithKey.
确保在目标 table 上定义了 PK。或者在填充之前清除 table,但要处理 table 参与的 FK 和关系(如果有的话)。
我认为你应该调用适配器下可用的更新方法。