0

I make an application in vb.net code using the visual basic 2010 environment and i have a little problem whith the datagridview. For more details, i have connected a postgresql database using the connector odbc and it work fine. I can insert, update, delete datas easily but after an update and only after an update query the datagridview display all my data but the row updated is moved to the last position automatically. Why ? if i look my database in PGadmin 3, i don't have this problem, it show me the rows sorted ascending.Personally I have searched solutions but i found anything. I will explain my situation with screenshot:

Here is my application when the form load: 1st image=> https://drive.google.com/file/d/0B_Lx61Af8AuUNUs4TDlWMnBIblE/view?usp=sharing

As u can see rows are sorted ascending. Here's the code of the "accueil" form :

Imports System.Data.Odbc

Public Class accueil

    Dim database As String = "Driver={PostgreSQL Unicode};Server=localhost;Port=5432;Database=formation;Uid=postgres;Pwd=test;"

    Dim CON As OdbcConnection
    Dim CMD As OdbcCommand
    Dim RD As OdbcDataReader
    Dim stock_id As Integer


    ''''Fonction pour l'affichage pour SessionFormation
    Function SessionFormationReadData() As Boolean

        Try
            CON = New OdbcConnection(database)
            CON.Open()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            CON.Close()
            End

            Exit Function
        End Try

        CMD = CON.CreateCommand()
        CMD.CommandText = "SELECT * FROM sessionformation;"
        RD = CMD.ExecuteReader()

        DataGridView1.Columns.Clear()
        DataGridView1.Rows.Clear()



        If (RD.Read()) Then

            DataGridView1.ColumnCount = 4

            DataGridView1.Columns(0).Name = "N° de Session"
            'DataGridView1.Columns(0).ValueType = GetType(Integer)

            DataGridView1.Columns(1).Name = "Test"
            'DataGridView1.Columns(1).ValueType = GetType(String)

            DataGridView1.Columns(2).Name = "Date de début"
            'DataGridView1.Columns(2).ValueType = GetType(DateTime)

            DataGridView1.Columns(3).Name = "Date de fin"
            'DataGridView1.Columns(3).ValueType = GetType(DateTime)


            DataGridView1.Rows.Add(RD("id_session"), RD("type"), RD("date_debut"), RD("date_fin"))


            While (RD.Read())

                DataGridView1.Rows.Add(RD("id_session"), RD("type"), RD("date_debut"), RD("date_fin"))


            End While

            'DataGridView1.Sort(DataGridView1.Columns(0), System.ComponentModel.ListSortDirection.Ascending)

            RD.Close()
            CON.Close()
            Return True

        Else

            RD.Close()
            CON.Close()
            Return False

        End If

    End Function


    ''''Fonction pour supprimer une session
    Public Sub SessionFormationDeleteData()

        stock_id = DataGridView1.CurrentRow.Cells(0).Value

        Try
            CON = New OdbcConnection(database)
            CON.Open()
            CMD = CON.CreateCommand()
            CMD.CommandText = "DELETE FROM SessionFormation WHERE ID_session= '" & stock_id & "';"
            CMD.ExecuteNonQuery()
            CON.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            CON.Close()
        End Try


    End Sub


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

        Call SessionFormationReadData()


    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ajouter.Show()

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        modifier.Show()

    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Call SessionFormationDeleteData()
        Call SessionFormationReadData()

    End Sub


End Class

Now i want to do an update to the first row, so i select it and i click on the "modifier" button. It's the image from the top here=> https://drive.google.com/file/d/0B_Lx61Af8AuUNW45dzFmWHMwcG8/view?usp=sharing

Here's the code of the "modifier" form :

Imports System.Data.Odbc
Public Class modifier

    Dim database As String = "Driver={PostgreSQL Unicode};Server=localhost;Port=5432;Database=formation;Uid=postgres;Pwd=test;"

    Dim CON As OdbcConnection
    Dim CMD As OdbcCommand


    ''''Affiche les données des cellules de la ligne sélection sur HOME dans les champs respectifs
    Private Sub modifier_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        TextBox1.Text = accueil.DataGridView1.CurrentRow.Cells(1).Value.ToString()
        DateTimePicker1.Text = accueil.DataGridView1.CurrentRow.Cells(2).Value.ToString()
        DateTimePicker2.Text = accueil.DataGridView1.CurrentRow.Cells(3).Value.ToString()

    End Sub


    ''''Fonction pour mettre à jour la base de donnée suite à une modification
    Public Sub SessionFormationUpdateData()
        Dim stock_id As Integer

        stock_id = accueil.DataGridView1.CurrentRow.Cells(0).Value

        Try
            CON = New OdbcConnection(database)
            CON.Open()
            CMD = CON.CreateCommand()
            CMD.CommandText = "update sessionformation set type= '" + TextBox1.Text.ToString() + "', date_debut='" + DateTimePicker1.Value + "', date_fin='" + DateTimePicker2.Value + "' where id_session= '" & stock_id & "';"
            CMD.ExecuteNonQuery()
            CON.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            CON.Close()
        End Try


    End Sub


    ''''Bouton "Ok" pour valider les modifications et rafraichir l'affichage sur "HOME"
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Call SessionFormationUpdateData()

        Close()

        Call accueil.SessionFormationReadData()


    End Sub


    ''''Fermeture de la fenêtre
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Close()

    End Sub

End Class

On the "modifier" when i click on the "ok" button, the "modifier" form close and on the "accueil" form the datagridview refresh all the data of the database. And look the results on the image from the bottom from the second link. The row with the id "18" moves to the last position on the datagridview. I don't know why.

Please i need some help. Thank to have read my long post and thank to the help. And also sorry for my bad english :-).

Paolo
  • 2,224
  • 1
  • 15
  • 19
  • possible duplicate of [Why do results from a SQL query not come back in the order I expect?](http://stackoverflow.com/questions/10999913/why-do-results-from-a-sql-query-not-come-back-in-the-order-i-expect) – Paolo May 21 '15 at 14:06
  • Thank you for the answer, but i don't think that's the problem. Because it's only on the vb.net application there is this problem. When i use pgadmin 3 to see my tables of my database i don't have this problem. All the datas are in the good order. – dimstheonlyone May 21 '15 at 14:41

1 Answers1

0

this is an expected behaviour as stated by postgresql documentation:

8.If the ORDER BY clause is specified, the returned rows are sorted in the specified order. If ORDER BY is not given, the rows are returned in whatever order the system finds fastest to produce.

if you don't specify a order by clause then the server may change the order at any run (the evaluations behind 'fastest to produce' may include system load, disk access, weather, time of the day, ...).

also be aware that this is by design (it is not a flaw) or a behaviour specific of postgresql, this is the normal and expected behaviour for many RDBMS because of the relational model.

if you need a specific order of the result set or you want a dependable order of that set then you must use a order by clause.

Paolo
  • 2,224
  • 1
  • 15
  • 19