Donate

DataGridview Paging Using BindingSource And BindingNavigator In VB.NET

Hi,

In reference to the previous post on DataGridView paging using C# Datagridview paging using BindingSource in C#, I developed a Visual Basic.NET version for VB.NET Developers.
Main Form Class:
 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
Option Infer On  
      Imports System.Configuration  
      Imports System.ComponentModel  
      Imports MySql  
      Imports MySql.Data  
      Imports MySql.Data.MySqlClient  
      Public Class FBinding  
        Public Property TotalRecords() As Integer  
        Public Const PageSize = 10  
       Private sourceData As New List(Of String)  
       Private dtSource As New DataTable  
       Dim page As New PageOffsetList()  
       Public Sub New()  
         ' This call is required by the designer.  
         InitializeComponent()  
         ' Add any initialization after the InitializeComponent() call.  
         Me.bindingNav.BindingSource = bindingWebsite  
         AddHandler Me.bindingWebsite.CurrentChanged, AddressOf Me.bindingWebsite_CurrentChanged  
         SetSource()  
       End Sub  
       Private Sub FBinding_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
         Me.bindingWebsite.DataSource = page  
       End Sub  
       Private Sub bindingWebsite_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bindingWebsite.CurrentChanged  
         Dim offset As Integer = CType(Me.bindingWebsite.Current, Int32)  
         Dim records As New List(Of Record)  
         Dim i As Integer = offset  
         While i < offset + PageSize AndAlso i < TotalRecords  
           records.Add(New Record() With  
           { _  
             .Website = sourceData(i).ToString() _  
           })  
           i += 1  
         End While  
         dgWebsites.DataSource = records  
       End Sub  
       Private Sub SetSource()  
         Dim sql As String = "select trim(website_host), url_host from table_website order by trim(website_host) asc"  
         Dim conn As MySqlConnection  
         conn = Nothing  
         Try  
           Dim connection As String = ConfigurationManager.AppSettings("WebsiteConnection").ToString()  
           conn = New MySqlConnection(connection)  
           conn.Open()  
           Dim cmd As New MySqlCommand(sql, conn)  
           Dim da As New MySqlDataAdapter(cmd)  
           da.Fill(dtSource)  
           If dtSource.Rows.Count Then  
             For Each item As DataRow In dtSource.Rows  
               If Not String.IsNullOrEmpty(item(0).ToString()) AndAlso Not String.IsNullOrEmpty(item(1).ToString()) Then  
                 sourceData.Add(item(0).ToString() & ", " & item(1).ToString())  
               End If  
             Next  
           End If  
           TotalRecords = sourceData.Count  
           conn.Close()  
         Catch ex As Exception  
           conn.Close()  
           conn.Dispose()  
         End Try  
       End Sub  
     End Class  

PageOffList Class:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
Option Infer On  
 Imports System.ComponentModel  
 Public Class PageOffsetList  
   Implements IListSource  
   Public ReadOnly Property ContainsListCollection() As Boolean _  
     Implements System.ComponentModel.IListSource.ContainsListCollection  
     Get  
       Return False  
     End Get  
   End Property  
   Public Function GetList() As System.Collections.IList _  
     Implements System.ComponentModel.IListSource.GetList  
     Dim pageOffset As New List(Of Int32)  
     Dim offset As Integer = 0  
     While offset <= FBinding.TotalRecords  
       pageOffset.Add(offset)  
       offset = offset + FBinding.PageSize  
     End While  
     Return pageOffset  
   End Function  
 End Class 

Record Class:
1
2
3
Public Class Record  
   Public Property Website As String  
 End Class
Output:
DataGridview Paging Using BindingSource And BindingNavigator In VB.NET
Source code available for download at Github
Cheers!

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Bootstrap Modal In ASP.NET MVC With CRUD Operations