Optimiser affichage données dans un datagrid
Bonjour
Je souhaiterais avoir votre avis pour améliorer mon code.
Je débute en vb.net mais mon code marche, je souhaite simplement améliorer et savoir si j'utilise la meilleure méthode.
J'ai simplifié mon code en gardant que les grandes lignes, mais en gros, ma base de données contient environ 15000 lignes avec une vingtaine de colonnes.
J’affiche mes données dans un datagrid, dans chaque ligne il y a des icônes, des combobox, des cellules de couleurs différentes
J’affiche mes lignes au fur et à mesure du scroll mais l'actualisation de l'affichage n'est pas très fluide et rapide,
Je parcourant le net j’ai peut-être plusieurs pistes pour améliorer :
utiliser un dataAdapteur au lieu du datareader
utiliser le virtualmode ?
Voilà si quelqu’un peut me donner des conseils ?
Merci
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| Sub Lecture_DataBase()
Dim Connection As New OleDb.OleDbConnection()
Dim DataReader As OleDbDataReader
Connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Chemin_DataBase & Fichier_DataBase
Connection.Open()
Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM `PRODUITS`", Connection)
DataReader = cmd.ExecuteReader
While DataReader.Read()
tableProduit.Rows.Add()
tableProduit.Rows(tableProduit.Rows.Count - 1).Item("path") = DataReader("path").ToString
tableProduit.Rows(tableProduit.Rows.Count - 1).Item("description") = DataReader("description").ToString
tableProduit.Rows(tableProduit.Rows.Count - 1).Item("fournisseur") = DataReader("fournisseur").ToString
tableProduit.Rows(tableProduit.Rows.Count - 1).Item("type") = 1
End While
Connection.Close()
LigneEcriture = 0
Affichage_DataGrid()
End Sub
Private Sub DataGridView1_Scroll(sender As Object, e As ScrollEventArgs) Handles DataGridViewMain.Scroll
If e.ScrollOrientation = ScrollOrientation.VerticalScroll Then
Dim displayedRowsCount As Integer = DataGridViewMain.DisplayedRowCount(True)
Dim firstDisplayedRowIndex As Integer = DataGridViewMain.FirstDisplayedScrollingRowIndex
Dim missingRowsCount As Integer = 90
If firstDisplayedRowIndex + displayedRowsCount >= LigneEcriture Then Affichage_DataGrid()
End If
End Sub
Sub Affichage_DataGrid()
With Main.col0 : .HeaderText = "" : .Name = "N1" : .Width = 25 : .Visible = True : .SortMode = DataGridViewColumnSortMode.NotSortable : End With
With Main.col1 : .HeaderText = "path" : .Name = "path" : .Width = 250 : .Visible = True : .SortMode = DataGridViewColumnSortMode.NotSortable : End With
With Main.col2 : .HeaderText = "description" : .Name = "description" : .Width = 250 : .Visible = True : .SortMode = DataGridViewColumnSortMode.NotSortable : End With
With Main.col9 : .HeaderText = "fournisseur" : .Name = "fournisseur" : .Width = 100 : .Visible = True : .SortMode = DataGridViewColumnSortMode.NotSortable : End With
If tableProduit.Rows.Count - 1 < LigneEcriture + 49 Then
max = tableProduit.Rows.Count - 1
Else
max = LigneEcriture + 49
End If
For t = LigneEcriture To max
Main.DataGridViewMain.Rows.Insert(LigneEcriture)
If tableProduit.Rows(t).Item("type") = 1 Then Main.DataGridViewMain.Rows(LigneEcriture).Cells("N1").Value = My.Resources.ZC_img_asm
Main.DataGridViewMain.Rows(LigneEcriture).Cells("path").Value = tableProduit.Rows(t).Item("path")
Main.DataGridViewMain.Rows(LigneEcriture).Cells("description").Value = tableProduit.Rows(t).Item("description")
Main.DataGridViewMain.Rows(LigneEcriture).Cells("fournisseur").Value = tableProduit.Rows(t).Item("fournisseur")
If Not IO.File.Exists(tableProduit.Rows(t).Item("path").ToString) Then Main.DataGridViewMain.Rows(LigneEcriture).Cells("path").Style.BackColor = Color.FromArgb(255, 200, 200) End If
LigneEcriture = LigneEcriture + 1
Next
End Sub |