4

我在寻找刷新 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

这是我的添加按钮...单击它后...数据已成功添加但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
4

9 回答 9

4

希望这将有助于创建功能

        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
于 2017-03-28T09:02:22.940 回答
3

我认为问题在于您正在向数据库添加一个新条目,而不是数据网格所代表的数据结构。您只是在加载事件中查询数据库中的数据,因此如果数据库在此之后发生更改,您将不会知道它。

要解决此问题,您需要在每次插入后重新查询数据库,或者在每次插入后将项目添加到除了 Access 表之外的表(0)数据结构。

于 2013-08-12T16:19:21.513 回答
2

如果您尝试使用数据集中的更新数据刷新绑定的 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()
于 2015-05-31T20:11:05.183 回答
0

您可以使用绑定源绑定到您的 datagridview。设置您的类或数据列表。设置一个 bindingsource.datasource 等于那个。将 datagridview 的数据源设置为绑定源。

于 2014-03-13T18:26:24.557 回答
0

这将重新加载数据网格视图:

Me.ABCListTableAdapter.Fill(Me.ABCLISTDATASET.ABCList)

希望这可以帮助

于 2013-10-31T20:54:13.333 回答
0

如果您使用formview或类似的东西,您也可以在databind事件中使用。像下面gridviewiteminsertedformview

protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        GridView1.DataBind();
    }

您也可以在数据源上执行此操作iteminserted

于 2015-02-27T17:02:48.577 回答
0

重新加载表格

Form1_Load(sender, e)
于 2017-01-17T14:04:12.103 回答
0

在将更改保存到数据库的按钮代码中,例如更新按钮,添加以下代码行:

MyDataGridView.DataSource = MyTableBindingSource

MyDataGridView.Update()

MyDataGridView.RefreshEdit()
于 2020-01-07T13:00:06.450 回答
-3
this.tablenameTableAdapter.Fill(this.databasenameDataSet.tablename)
于 2016-04-05T11:27:59.500 回答