我在寻找刷新datagridview的方法时遇到了很多麻烦..我已经尝试过datagridview.refresh(),datagridview.Update()....但它不起作用......
这是我的代码
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
我尝试过这个,但它也不起作用。
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
这是我的ADD按钮...点击后...数据已成功添加,但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
答案 0 :(得分:3)
我认为问题在于您要向数据库添加新条目,而不是数据网格表示的数据结构。您只是在数据库中查询load事件中的数据,因此如果数据库发生更改,您将无法了解它。
要解决此问题,您需要在每次插入后重新查询数据库,或者在每次插入后将项目添加到tables(0)数据结构以及Access表。
答案 1 :(得分:2)
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
答案 2 :(得分:1)
尝试
DataGrid.Items.Refresh();
喜欢这个答案
How Can I refresh my Datagrid on WPF automatically for every 1 minute?
抱歉,它应该是DataGrid
而不是DataGridView
它看起来应该是
ShowData.Items.Refresh();
假设这是您的DataGrid对象
答案 3 :(得分:1)
如果您尝试使用数据集中的更新数据刷新绑定数据网格视图,我发现此代码可用。显然,这是在我将更新发送到数据库之后。
'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()
答案 4 :(得分:0)
这会重新加载datagridview:
Me.ABCListTableAdapter.Fill(Me.ABCLISTDATASET.ABCList)
希望这有帮助
答案 5 :(得分:0)
您可以使用绑定源绑定到您的datagridview。 设置您的类或数据列表。 将bindingsource.datasource设置为等于该值。 将datagridview的数据源设置为bindingsource。
答案 6 :(得分:0)
如果您使用formview
或类似的内容,则databind
的{{1}}事件gridview
iteminserted
也可以formview
。如下所示
protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
GridView1.DataBind();
}
您也可以在数据源iteminserted
上执行此操作。
答案 7 :(得分:0)
重新加载表单
Form1_Load(sender, e)
答案 8 :(得分:0)
在将更改保存到数据库的按钮的代码(例如,更新按钮)中,添加以下代码行:
MyDataGridView.DataSource = MyTableBindingSource
MyDataGridView.Update()
MyDataGridView.RefreshEdit()
答案 9 :(得分:-3)
this.tablenameTableAdapter.Fill(this.databasenameDataSet.tablename)