How to update my datagridview after adding new data

I have many problems finding ways to update my datagridview. I tried datagridview.refresh (), datagridview.Update () .... but it does not work ...

here is 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 tried this one but it doesn't work either.

 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 clicked ... the data was added successfully, but the datagridview is not updating ...

 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
source share
9 answers

I think the problem is that you are adding a new record to the database, but not to the data structure that the datagrid represents. You only query the database for the data in the load event, so if the database changes after this, you will not know about it.

To solve the problem, you need to either query the database again after each insertion, or add an element to the table structure (0) in addition to the Access table after each insertion.

+3
source

Wish this helps Create a 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 
+2
source

try

 DataGrid.Items.Refresh(); 

as on this answer

How can I update my Datagrid in WPF automatically every 1 minute?

Sorry, it should be a DataGrid , not a DataGridView

it looks like it should be

 ShowData.Items.Refresh(); 

Assuming this is your DataGrid object

+1
source

I found this code to work if you are trying to update the associated datagridview with updated data from the dataset. Obviously, this was after I sent the update to the database.

 '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() 
+1
source

This reloads the datagridview:

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

Hope this helps

0
source

You can use the binding source to bind to your datagridview. Define your class or list of data. Set the bindingsource.datasource source for it. Set the data source of your datagridview document for your binding source.

0
source

If you are using formview or something similar, you can databind gridview in the iteminserted formview event too. As below

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

You can do this in the iteminserted data source as iteminserted .

0
source

reload form

 Form1_Load(sender, e) 
0
source
 this.tablenameTableAdapter.Fill(this.databasenameDataSet.tablename) 
-3
source

Source: https://habr.com/ru/post/1496628/


All Articles