Home > Back-end >  Stuttering DataGridView in VB
Stuttering DataGridView in VB

Time:04-03

I have a DataGridView that is bound to a datatable with hundred of rows, the database is a simple flatfile database written to a txt file. Whenever I scroll to the bottom the DGV starts stuttering. I am thinking of solutions but cannot find a way to code them

Here are my proposed solution:

  • Use Paging to lessen the numbers of row being rendered. Here's is a similar solution but they are using sql
  • Using doublebuffer which I've never touched before. I've tried doing DGV.doublebuffer = true but it said DGV is protected

Any help or clarification on my problem are greatly appreciated

Edit: Here is a GIF of how my DGV is stutteting

The Datatable is named Tbl_Sample

Here is how I insert rows of data into data table. It gets data using System.IO on the Flatfile database(.txt file), split each line then send it to InputTbl as a row

Public Sub Update_Table(InputTbl As DataTable, InputFile As String)
    InputTbl.Rows.Clear()
    Dim lines() As String
    Dim vals() As String
    lines = File.ReadAllLines(InputFile)
    For i = 0 To lines.Length - 1
        vals = lines(i).ToString().Split("|")
        Dim row(vals.Length - 1) As String
        For j = 0 To vals.Length - 1
            row(j) = vals(j).Trim()
        Next j
        InputTbl.Rows.Add(row)
    Next i
End Sub

I set the table as Data Source for the DGV by DGV.DataSource = Tbl_Sample

The Datatable is created as Follow

 Public Sub Sample_Table()
    'Method For creating database file
    Create_DBFile("Sample.db")

    Try
        Tbl_Sample.Columns.Add("ID", Type.GetType("System.Int32"))
        Tbl_Sample.Columns.Add("Name", Type.GetType("System.String"))
        Tbl_Sample.Columns.Add("Username", Type.GetType("System.String"))
        Tbl_Sample.Columns.Add("Account_Type", Type.GetType("System.String"))
        Tbl_Sample.Columns.Add("Date", Type.GetType("System.String"))
        Tbl_Sample.Columns.Add("Time", Type.GetType("System.String"))
    Catch ex As Exception
    End Try
    Update_Table(Tbl_Sample, "Sample.db") 'populate table
End Sub

The way I create columns is not the best. I just copied it from my old program

CodePudding user response:

Solution link here

Imports System.Reflection

Public Sub EnableDoubleBuffered(ByVal dgv As DataGridView)
    Dim dgvType As Type = dgv.[GetType]()
    Dim pi As PropertyInfo = dgvType.GetProperty("DoubleBuffered", _
                                             BindingFlags.Instance Or BindingFlags.NonPublic)
    pi.SetValue(dgv, True, Nothing)
End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    EnableDoubleBuffered(MyDataGridView, True)
End Sub
  • Related