VB. NET use the datagridview page display

Source: Internet
Author: User

When you use the datagridview datagrito display records from the datatable, if there are too many records (hundreds of records) in the datatable, it is inconvenient to use the datagridview to view records. If you can display the records by page, only 10 entries are displayed on each page (you can specify the number of entries displayed on each page at will), and there are many jump buttons between pages? Now let's talk about how I did it.

I have compiled a class to help the records of the datagridview control display by PAGE and provide the page Jump method. The source code of this class is as follows:

Http://download1.csdn.net/down3/20070610/10102852142.vb

Public class clsdatagridviewpage

'Number of records per page
Private _ rowsperpage as integer
'Total number of pages
Private _ totalpage as integer
'Current page number
Private _ curpage as integer = 0
'The datagridview to be paged
Private _ datagridview as windows. Forms. datagridview
'And the dataview to be displayed by page
Private _ DV as dataview

'Retrieve and set the number of records per page
Public property rowsperpage () as integer
Get
Return _ rowsperpage
End get
Set (byval value as integer)
_ Rowsperpage = Value
End set
End Property

'Retrieve the total number of pages
Public readonly property totalpage () as integer
Get
Return _ totalpage
End get
End Property

'Get and set the current page number
Public property curpage () as integer
Get
Return _ curpage
End get
Set (byval value as integer)
_ Curpage = Value
End set
End Property

'Set the getdview dview to be paged
Public writeonly property setdatagridview ()
Set (byval value as object)
_ Datagridview = Value
End set
End Property

'Set the dataview to be displayed by page.
Public writeonly property setdataview ()
Set (byval value as object)
_ DV = Value
End set
End Property

Public sub new ()

End sub

'Reload the new function and assign values to members during construction.
Public sub new (byval datagridview as windows. Forms. datagridview, byval DV as dataview ,_
Byval rowsperpage as integer)
_ Datagridview = datagridview
_ DV = dv
_ Rowsperpage = rowsperpage
End sub

'Start pagination
Public sub paging ()
'First, determine whether the number of records in dataview is sufficient to form multiple pages,
'If you cannot, there will be only one page, and the records to be displayed in the datagridview are equivalent to the records on the last page.
If _ DV. Count <= _ rowsperpage then
_ Totalpage = 1
Golastpage ()
Exit sub
End if

'If it can be divided into multiple pages, it is necessary to calculate the total number of pages, and then the datagridview displays the first page
If _ DV. Count mod _ rowsperpage = 0 then
_ Totalpage = int (_ DV. Count/_ rowsperpage)
Else
_ Totalpage = int (_ DV. Count/_ rowsperpage) + 1
End if
Gofirstpage ()
End sub

'To the first page
Public sub gofirstpage ()
'If there is only one page, the displayed record is equivalent to the "Last page" record.
If _ totalpage = 1 then
Golastpage ()
Exit sub
End if
'If there are multiple pages, Go to page 1.
_ Curpage = 0
Gonopage (_ curpage)
End sub

Public sub gonextpage ()
'This code is mainly used to prevent overflow of the current page number.
_ Curpage + = 1
If _ curpage> _ totalpage-1 then
_ Curpage = _ totalpage-1
Exit sub
End if

'If the last page is reached, the records on the last page will be displayed.
If _ curpage = _ totalpage-1 then
Golastpage ()
Exit sub
End if

'If the last page is not reached, the specified "next page" will be reached"
Gonopage (_ curpage)
End sub

Public sub goprevpage ()
'Prevent invalid current page number
_ Curpage-= 1
If _ curpage <0 then
_ Curpage = 0
Exit sub
End if

'To the specified "Previous Page"
Gonopage (_ curpage)
End sub

'To the last page
Public sub golastpage ()
_ Curpage = _ totalpage-1
Dim I as integer
Dim dT as new datatable
'Dt is only a temporary able, used to obtain records of the required number of pages
Dt = _ DV. totable. Clone

For I = (_ totalpage-1) * _ rowsperpage to _ DV. Count-1
The upper and lower limits of the 'I value are critical. During debugging, an error is often reported and the line cannot be found.
'It is because the I value overflows.
Dim Dr as datarow = DT. newrow
Dr. itemarray = _ DV. totable. Rows (I). itemarray
DT. Rows. Add (DR)
Next
_ Datagridview. datasource = dt
End sub

'To the specified page
Public sub gonopage (byval pageno as integer)
_ Curpage = pageno
'Prevents invalid page numbers
If _ curpage <0 then
Msgbox ("page number cannot be less than 1 ")
Exit sub
End if

'Prevent page number Overflow
If _ curpage> = _ totalpage then
Msgbox ("the page number exceeds the upper limit ")
Exit sub
End if

'If the page number is the last page, the last page is displayed.
If _ curpage = _ totalpage-1 then
Golastpage ()
Exit sub
End if

'Not the last page. The page with the specified page number is displayed.
Dim dT as new datatable
Dt = _ DV. totable. Clone
Dim I as integer
For I = pageno * _ rowsperpage to (pageno + 1) * _ rowsperpage-1
Dim Dr as datarow = DT. newrow
Dr. itemarray = _ DV. totable. Rows (I). itemarray
DT. Rows. Add (DR)
Next
_ Datagridview. datasource = dt
End sub
End Class

The following is an example of how to use this class (referred to as the paging auxiliary class) to help the datagridview control display records by page:

First, add the pagination helper class member to the form class code of the datagridview:

Private dgvpage as clsdatagridviewpage

Then, instantiate the member in the form constructor code:

Public sub new ()

'The call is required by the Windows Form Designer.
Initializecomponent ()

'Add any initialization after initializecomponent () is called.

'Instantiate a paging class member
Dgvpage = new clsdatagridviewpage 'pagination class member
End sub

Note: To display records by page in the datagridview, the three most important attributes of the paging class must be set:
Setdatagridview this attribute is used to set the datagridview Control for display records by page on the form.
Rowsperpage: This attribute is used to set the number of records to be displayed on each page.
Setdataview this attribute is used to set the dataview to be displayed in the datagridview Space
You can flexibly set these three attributes to enable different dview to specify the number of records per page for different dataviews (is it convenient ?) The following example shows how to set these three attributes:

'Sets the attributes of a paging object.
Dgvpage. getdatagridview = me. datagridview1 'requires me. datagridview1 to be paged.
Dgvpage. rowsperpage = 10' 10 records per page

'Obtain the dataview to be displayed by page.
Dim dT as datatable = me. datagridview1.datasource
Dgvpage. setdataview = DT. defaultview

The preceding attribute setting process is performed when the data source of the datagridview is set for the first time. (Because the paging class achieves the paging display of the datagridview by changing the datasource attribute of the datagridview. Of course, you can assign the dataview to dgvpage. setdataview in the datagridview at any time, for example:

Dim DV as new dataview = ......
Dgvpage. setdataview = dv

In this way, the records displayed in the datagridview can be dynamically changed .)

The rest is simple: to display records on the page of the datagridview, call the paging method of the paging class,
To view the first page, call the gofirstpage method of the paging class.
To view the next page, call the gonextpage method.
To view the previous page, call the goprevpage method.
To view the last page, call the golastpage method.
To view the page number, call the gonopage method.

As long as you use this class flexibly, the paging function of your datagridview control is no problem. And you can always know the current page (curpage () + 1), the total number of pages (totalpage ())

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.