【问题标题】:Datagridview : row moves after an update, why?Datagridview:更新后行移动,为什么?
【发布时间】:2015-08-03 05:14:31
【问题描述】:

我使用 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 上的最后一个位置。我不知道为什么。

我需要一些帮助。 感谢阅读我的长篇文章并感谢帮助。 也很抱歉我的英语不好:-)。

【问题讨论】:

标签: .net vb.net postgresql datagridview odbc


【解决方案1】:

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

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

如果您没有指定order by 子句,那么服务器可能会在任何运行时更改顺序(“最快生产”背后的评估可能包括系统负载、磁盘访问、天气、一天中的时间...... .).

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

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

【讨论】:

  • 不客气。如果您觉得答案有用,请点赞或标记为答案。
猜你喜欢
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 2016-09-03
  • 1970-01-01
  • 2016-12-12
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多