Refresh DataGridView

T

thomasp

This is a two part question,

1) The code below should display a form with a datagridview and a few
command buttons. This form should allow the user to make change to the
records displayed
in the datagridview and have to changes written to the Filters table in
an MS Access database. The if the user chooses, clicking on the Restore
button empties
the contents of the Filters table, refills it with the contents of the
DefaulFilters Table, and refreshes the contents of the Datagridview to
reflect that the defaults
have been reloaded. All of this works except the part where the
datagridview gets refreshed. I can close the form and reopen it and the
default data is loaded,
but it is not loaded without closing and reopening the form. How can I
refresh the datagrid view without closing and opening the form?

2) This one may be a bit harder. The code in the Restore sub below was
built by coping, pasting, and modifing examples in the newsgroups as is most
of my code.
If someone has time, could you please write a few lines that explain
what each of the line of code are doing. I follow that a connection to
the database is open,
two DataAdapters are open for each of the two database tables, these
DataAdapters are used to fill two DataSets, and then I get lost.

Thanks for any help with either of the above.

Thomas

Private Sub frmFilters_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load, MyBase.Load

Me.FiltersTableAdapter.Fill(Me.TAMDataSet.Filters)

End Sub

Private Sub cmdRestore_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdRestore.Click

'This sub empties the Filters Table. Copies the records in the
DefaultFilters table
'and places them them in the Filters Table. Then it refreshes the
datagridviewer with the
'new records.
Dim myConnection As New OleDbConnection(strConn)
Dim ds As DataSet = New DataSet
Dim ds2 As DataSet = New DataSet
Dim tblDefaultFilters As DataTable
Dim tblCopyDefaultFilters As DataTable


'Delete all records in the FILTERS Table
strSQL = "DELETE * FROM FILTERS"

cmd = New OleDb.OleDbCommand(strSQL, cn)

rdr = cmd.ExecuteReader



myConnection.Open()

Dim da As OleDb.OleDbDataAdapter = New
OleDb.OleDbDataAdapter("Select * from DefaultFilters", myConnection)
Dim da2 As OleDb.OleDbDataAdapter = New
OleDb.OleDbDataAdapter("Select * from Filters", myConnection)

da.Fill(ds)
da2.Fill(ds2)
tblDefaultFilters = ds.Tables(0)

Dim CB As OleDb.OleDbCommandBuilder = New
OleDb.OleDbCommandBuilder(da2)

tblCopyDefaultFilters = tblDefaultFilters.Copy()

tblCopyDefaultFilters = CType(tblDefaultFilters.Clone(), DataTable)

For Each row As DataRow In tblDefaultFilters.Rows
Dim newRow As DataRow = tblCopyDefaultFilters.NewRow()
For Each col As DataColumn In tblDefaultFilters.Columns
newRow(col.ColumnName) = row(col.ColumnName)
Next col
tblCopyDefaultFilters.Rows.Add(newRow)
Next row

da2.Update(tblCopyDefaultFilters)


'Clear and refill the dataset and data adapter
'then refresh the datagrid
Me.TAMDataSet.Clear()
Me.FiltersTableAdapter.Fill(Me.TAMDataSet.Filters)
Me.DataGridView1.Refresh()


myConnection.Close()



End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top