Results 1 to 10 of 10

Thread: Reload DataGridView

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    24

    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 ????

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    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:
    1. 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.

  3. #3
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    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.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    24

    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

  5. #5
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Reload DataGridView

    Is newDate datacolumn a System.DateTime type?
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    24

    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 :-(

  7. #7
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Reload DataGridView

    Try putting this code
    Code:
    ds.Tables("BankStatement").DefaultView.Sort = "newDate desc"
    right above
    Code:
    da.Update(ds, "BankStatement")
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    24

    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

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    24

    Re: Reload DataGridView

    Is it because we aren't binding/sorting the datagridview, in this case DataGridFD ???

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    24

    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
  •  



Click Here to Expand Forum to Full Width