|
-
Mar 27th, 2016, 01:05 PM
#1
Thread Starter
Junior Member
Reload DataGridView
Ok this is driving me crazy, i am loading a datagridview sorted by date as follows.....
Public Sub LoadDatabaseDetails()
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
theDatabase = "\FirstDirect.mdb"
myDocumentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
fullDatabasPath = myDocumentsFolder & theDatabase
dbSource = "Data Source = " & fullDatabasPath
con.ConnectionString = dbProvider & dbSource
con.Open()
da = New OleDb.OleDbDataAdapter("SELECT * FROM tblContacts ORDER BY newDate DESC", con)
da.Fill(ds, "BankStatement")
tables = ds.Tables
Dim view As New DataView(tables(0))
frmMaster.DataGridFD.DataSource = view
con.Close()
End Sub
I am adding new data without any issues but it always add's it to the bottom instead of Date order, i add using the following...
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("BankStatement").NewRow()
dsNewRow.Item("newDate") = txtDate.Text
dsNewRow.Item("Description") = txtDescription.Text
dsNewRow.Item("AmountIn") = txtAmountIn.Text
dsNewRow.Item("AmountOut") = txtAmountOut.Text
ds.Tables("BankStatement").Rows.Add(dsNewRow)
da.Update(ds, "BankStatement")
MessageBox.Show("New Record added to the Database")
da.Fill(ds)
DataGridFD.ResetBindings()
End If
End Sub
It's taken me ages to figure out
da.Fill(ds)
DataGridFD.ResetBindings()
Is there a better was of reloading the Data ????
-
Mar 28th, 2016, 03:29 AM
#2
Re: Reload DataGridView
Hi,
Since the DataGridView is displaying your information with the Newest record at the top of the DGV why not use the InsertAt Method of the Datatable’s Rows collection to add the new record at the Top of the DataGridView? Ie:-
vb.net Code:
yourDT.Rows.InsertAt(someNewDataRow, 0)
This way you do not need to reload the table and reset your bindings after saving the new information to the database.
Hope that helps.
Cheers,
Ian
BTW, when using Dates you need to be using Date Types rather than Strings to get the results you will be expecting in the future.
-
Mar 28th, 2016, 05:42 AM
#3
Re: Reload DataGridView
Hi,
First, please surround your code snippets with code tags to make it readable.
How about performing sort operation on the table after insertion of new record? Make sure that your column is a date type.
Code:
ds.Tables("BankStatement").DefaultView.Sort = "newDate desc"
Reference: RESOLVED-Sorting-a-DataTable-DefaultView
- kgc
Last edited by KGComputers; Mar 28th, 2016 at 05:45 AM.
-
Mar 28th, 2016, 06:17 AM
#4
Thread Starter
Junior Member
Re: Reload DataGridView
Whilst adding the record and i have now changed the global variable to 'DATE' like..
Code:
Public todaysdate As Date
The record is still going to the bottom :-9
Code:
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
todaysdate = txtDate.Text
dsNewRow = ds.Tables("BankStatement").NewRow()
dsNewRow.Item("newDate") = todaysdate
dsNewRow.Item("Description") = txtDescription.Text
dsNewRow.Item("AmountIn") = txtAmountIn.Text
dsNewRow.Item("AmountOut") = txtAmountOut.Text
ds.Tables("BankStatement").Rows.Add(dsNewRow)
da.Update(ds, "BankStatement")
MessageBox.Show("New Record added to the Database")
ds.Tables("BankStatement").DefaultView.Sort = "newDate desc"
End If
-
Mar 28th, 2016, 09:20 AM
#5
Re: Reload DataGridView
Is newDate datacolumn a System.DateTime type?
-
Mar 28th, 2016, 10:17 AM
#6
Thread Starter
Junior Member
Re: Reload DataGridView
Hi
This is pulled from a Access Database as above which is entered as datatype Date/Time (Short Date)
I must admit, i have been pulling my hair out with this and i really thought there would be a simple answer :-(
-
Mar 28th, 2016, 10:40 AM
#7
Re: Reload DataGridView
Try putting this code
Code:
ds.Tables("BankStatement").DefaultView.Sort = "newDate desc"
right above
Code:
da.Update(ds, "BankStatement")
-
Mar 28th, 2016, 11:08 AM
#8
Thread Starter
Junior Member
Re: Reload DataGridView
Sorry getting really confused now, it's still going to the bottom, think i will have to rethink this whole thing, i have the following, not sure if ive messed up anything....
Code:
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("BankStatement").NewRow()
todaysdate = txtDate.Text
dsNewRow.Item("newDate") = todaysdate
dsNewRow.Item("Description") = txtDescription.Text
dsNewRow.Item("AmountIn") = txtAmountIn.Text
dsNewRow.Item("AmountOut") = txtAmountOut.Text
ds.Tables("BankStatement").Rows.Add(dsNewRow)
ds.Tables("BankStatement").DefaultView.Sort = "newDate desc"
da.Update(ds, "BankStatement")
MessageBox.Show("New Record added to the Database")
End If
End Sub
-
Mar 28th, 2016, 11:29 AM
#9
Thread Starter
Junior Member
Re: Reload DataGridView
Is it because we aren't binding/sorting the datagridview, in this case DataGridFD ???
-
Mar 29th, 2016, 12:30 PM
#10
Thread Starter
Junior Member
Re: Reload DataGridView
Ive done it, because the DataGridView is populated programatically i was able to use the Sort method after Adding a new record like...
DataGridFD.Sort(DataGridFD.Columns("newDate"), System.ComponentModel.ListSortDirection.Descending)
Works a treat :-)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|