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