Datagridview:更新后行移动,为什么?

时间:2015-05-21 13:37:08

标签: .net vb.net postgresql datagridview odbc

我使用visual basic 2010环境在vb.net代码中创建一个应用程序,我对datagridview有一点问题。有关更多详细信息,我已使用连接器odbc连接postgresql数据库,它工作正常。我可以轻松插入,更新,删除数据,但在更新后,只有在更新查询后,datagridview才会显示我的所有数据,但更新的行会自动移动到最后一个位置。为什么?如果我在PGadmin 3中查看我的数据库,我没有这个问题,它向我展示了升序排序的行。我个人搜索了解决方案,但我发现了什么。我将用截图解释我的情况:

这是表单加载时我的应用程序: 第一张图片=> https://drive.google.com/file/d/0B_Lx61Af8AuUNUs4TDlWMnBIblE/view?usp=sharing

你可以看到行按升序排序。 这是“accueil”形式的代码:

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

现在我想对第一行进行更新,所以我选择它并点击“修改器”按钮。这是顶部的图像=> https://drive.google.com/file/d/0B_Lx61Af8AuUNW45dzFmWHMwcG8/view?usp=sharing

以下是“修饰符”形式的代码:

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

在“修饰符”上,当我点击“确定”按钮时,“修饰符”形式关闭并在“accueil”形式上,datagridview刷新数据库的所有数据。并从第二个链接的底部查看图像上的结果。 id为“18”的行移动到datagridview上的最后一个位置。我不知道为什么。

我需要一些帮助。 感谢您阅读了我的长篇文章并感谢您的帮助。 对于我糟糕的英语也很抱歉: - )。

1 个答案:

答案 0 :(得分:0)

这是postgresql documentation所述的预期行为:

  

8.如果指定了ORDER BY子句,则返回的行按指定的顺序排序。如果未给出ORDER BY,则返回行   以任何顺序,系统发现最快生产。

如果您没有指定order by条款,那么服务器可能会在任何运行中更改订单(最快生成的评估可能包括系统加载,磁盘访问,天气,一天中的时间......)。

还要注意这是设计(它不是缺陷)或postgresql特有的行为,这是许多RDBMS的正常和预期行为,因为relational model

如果您需要结果集的特定顺序,或者您想要该集合的可靠顺序,则必须使用order by子句。

相关问题