vb.net 添加新数据后如何刷新我的数据网格视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18191666/
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
how to refresh my datagridview after I add new data
提问by hPys
I'm having a lot of trouble finding ways to refresh my datagridview.. I've tried datagridview.refresh(), datagridview.Update()....but it doesn't work...
我在寻找刷新 datagridview 的方法时遇到了很多麻烦。
here's my code
这是我的代码
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
Imports System.Data.DataTable
Public Class Form1
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim addstring As String
Dim cnn As OleDbConnection = New OleDbConnection
Dim ds As DataSet = New DataSet
Dim da As OleDbDataAdapter
Dim tables As DataTableCollection = ds.Tables
Dim cmd As New OleDb.OleDbCommand
Dim dr As System.Data.OleDb.OleDbDataReader
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
t_date.Text = Today
provider = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
dataFile = "C:\Users\hp-2\Documents\Visual Studio 2012\Projects\Delta\Delta.mdb"
connString = provider & dataFile
cnn.ConnectionString = connString
da = New OleDbDataAdapter("Select Customer_Name, Job, Amount from [Transaction] where Trans_date = Date()", cnn)
da.Fill(ds, "Transaction")
Dim view1 As New DataView(tables(0))
Dim source1 As New BindingSource()
source1.DataSource = view1
showdata.DataSource = view1
showdata.Refresh()
cnn.Close()
End Sub
I've tried this one but it doesn't work too.
我试过这个,但它也不起作用。
Private Sub showdat()
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
showdata.Refresh()
cnn.Close()
End Sub
...
...
Private Sub btmclose_Click(sender As Object, e As EventArgs) Handles btmclose.Click
Me.Close()
End Sub
Private Sub C_job_SelectedIndexChanged(sender As Object, e As EventArgs) Handles C_job.SelectedIndexChanged
Dim selected As String = C_job.SelectedItem.ToString()
If selected = "Internet" Then
t_amount.Text = "20"
php.Visible = True
ElseIf selected = "Games" Then
t_amount.Text = "10"
php.Visible = True
ElseIf selected = "Print (short)" Then
t_amount.Text = "1"
php.Visible = True
ElseIf selected = "Print (long)" Then
t_amount.Text = "2"
php.Visible = True
ElseIf t_amount.Text = "" Then
php.Visible = False
End If
End Sub
here is my ADD button... after i've click it...the data is successfully added but the datagridview doesn't refresh...
这是我的添加按钮...在我点击它之后...数据已成功添加但 datagridview 不刷新...
Private Sub btnadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click
provider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
dataFile = "C:\Users\hp-2\Documents\Visual Studio 2012\Projects\Delta\Delta.mdb"
connString = provider & dataFile
cnn.ConnectionString = connString
cnn.Open()
cmd.Connection = cnn
cmd.CommandText = "insert into [Transaction] (Customer_Name, Job, Trans_date, Amount ) " & _
" values ('" & C_name.Text & "','" & C_job.Text & "','" & t_date.Text & "','" & t_amount.Text & "')"
cmd.ExecuteNonQuery()
showdat()
cnn.Close()
End Sub
End Class
回答by Seth Moore
I think the problem is that you're adding a new entry to the database, but not the data structure that the datagrid represents. You're only querying the database for data in the load event, so if the database changes after that you're not going to know about it.
我认为问题在于您向数据库添加了一个新条目,而不是 datagrid 表示的数据结构。您只是在加载事件中查询数据库中的数据,因此如果数据库在此之后发生更改,您将不会知道它。
To solve the problem you need to either re-query the database after each insert, or add the item to tables(0) data structure in addition to the Access table after each insert.
要解决此问题,您需要在每次插入后重新查询数据库,或者在每次插入后将项目添加到除了 Access 表之外的表(0)数据结构中。
回答by taju
I found this code to work if you're trying to refresh a bound datagridview with updated data from a dataset. Obviously, this was after I sent the update to the database.
如果您尝试使用数据集中的更新数据刷新绑定的 datagridview,我发现此代码有效。显然,这是在我将更新发送到数据库之后。
'clear out the datasource for the Grid view
Me.DataGridView1.DataSource = Nothing
'refill the table adapter from the dataset table
Me.viewABCTableAdapter.Fill(Me.yourDataSet.viewABC)
'reset the datasource from the binding source
Me.DataGridView1.DataSource = Me.viewABCBindingSource
'should redraw with the new data
Me.DataGridView1.Refresh()
回答by user3705692
wish this will help Create Function
希望这将有助于创建功能
private sub loaddata()
datagridview.Datasource=nothing
datagridview.refresh
dim str as string = "select * from database"
using cmd As New OleDb.OleDbCommand(str,cnn)
using da As new OleDbDataAdapter(cmd)
using newtable as new datatable
da.fill (newtable)
datagridview.datasource=newtable
end using
end using
end using
end sub
回答by Malachi
try
尝试
DataGrid.Items.Refresh();
like on this answer
喜欢这个答案
How Can I refresh my Datagrid on WPF automatically for every 1 minute?
sorry it should be DataGrid
and not DataGridView
抱歉应该是DataGrid
而不是DataGridView
it looks like it should be
看起来应该是
ShowData.Items.Refresh();
assuming that is your DataGrid Object
假设这是您的 DataGrid 对象
回答by Marc Chemali
This reloads the datagridview:
这将重新加载数据网格视图:
Me.ABCListTableAdapter.Fill(Me.ABCLISTDATASET.ABCList)
Hope this helps
希望这可以帮助
回答by Corey Burton
You can use a binding source to bind to with your datagridview. Set your class or list of data. Set a bindingsource.datasource equal to that. Set the datasource of your datagridview to your bindingsource.
您可以使用绑定源与您的数据网格视图绑定。设置您的类或数据列表。设置一个 bindingsource.datasource 等于它。将您的 datagridview 的数据源设置为您的绑定源。
回答by user227103
If you using formview
or something similar you can databind
the gridview
on the iteminserted
event of the formview
too. Like below
如果您在使用formview
或类似的东西,你可以databind
在gridview
上iteminserted
的事件formview
了。像下面
protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
GridView1.DataBind();
}
You can do this on the data source iteminserted
too.
您也可以在数据源上执行此操作iteminserted
。
回答by Michael
In the code of the button that saves the changes to the database eg the update button, add the following lines of code:
在将更改保存到数据库的按钮(例如更新按钮)的代码中,添加以下代码行:
MyDataGridView.DataSource = MyTableBindingSource
MyDataGridView.Update()
MyDataGridView.RefreshEdit()
回答by RoganRicheart
reload the form
重新加载表单
Form1_Load(sender, e)
回答by user6161010
this.tablenameTableAdapter.Fill(this.databasenameDataSet.tablename)