Rabu, 22 Desember 2010

Paging DataGridView With LINQ


Buka project LatihanLINQ, dan tambahkan 1 form dan tambahkan 4 buah button, 1 label dan 1 DataGrdiView. Ubah properties dan atur letak komponen seperti diatas.
Form :
Name : F_Grid
Text : Paging Grid
StartPosition : CenterScreen
Button 1: Name = btnAwal , Text = <<
Button 2: Name = btnMundur, Text = <
Button 3: Name = btnMaju, Text = >
Button 4: Name = btnAkhir, Text = >>
Label : Name =lblStart, Text = Kosong
DatGridView1 : Name =DgView
Pada Project LatihanLINQ, buka class AccessSupplier.vb
Dan ketikkan sintak berikut didalam class AccessSupplier :
'Menampilkan jumlah baris table
Public Function GetTotalBaris() As Integer
Dim dc As New DataClassesDataContext
Dim hasil = (From sup In dc.GetTable(Of T_Supplier)() _
Select sup).ToList
Return hasil.Count 'menghitung jumlah element pada list
End Function
'menampilkan data dari n baris sebanyak n baris
Public Function GetSupplierRows(ByVal Awal As Integer, _
ByVal akhir As Integer) As List(Of T_Supplier)
Dim dc As New DataClassesDataContext
Dim hasil = (From sup In dc.GetTable(Of T_Supplier)() _
Order By sup.Sup_Code Select sup).Skip(Awal).Take(akhir).ToList
Return hasil
End Function
View Code untuk F_Grid, lalu ketikkan sintaks berikut di bawah Public Class F_Grid
Private AccessSupplier As New AccessData.AccessSupplier
Private NilAwal, NilTotal, NilBagi As Integer
Private NilPage As Integer = 2
Private Sub LabelRecord()
lblStart.Text = "Awal baris : " & (NilAwal + 1).ToString & " dari total : " + NilTotal.ToString
End Sub
Double click pada form F_Grid, dan ketikkan sintaks berikut :
Try
NilAwal = 0 'awal baris
NilTotal = AccessSupplier.GetTotalBaris 'mengambil total baris
DgView.DataSource = AccessSupplier.GetSupplierRows(NilAwal, NilPage) 'menampilkan data pada gridview
LabelRecord() 'menampilkan nilai baris dan total baris
Catch ex As Exception
MsgBox(ex.Message)
End Try
Double click button Maju dan ketik sintaks berikut :
Try
If (NilAwal + NilPage) <= NilTotal Then
NilAwal += NilPage
DgView.DataSource = AccessSupplier.GetSupplierRows(NilAwal, NilPage)
LabelRecord()
Else
MsgBox("Data sudah pada halaman terakhir", MsgBoxStyle.Information, "LINQ")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Button Mundur :
Try
If (NilAwal - NilPage) >= 0 Then
NilAwal -= NilPage
DgView.DataSource = AccessSupplier.GetSupplierRows(NilAwal, NilPage)
LabelRecord()
Else
MsgBox("Data sudah pada halaman pertama", MsgBoxStyle.Information, "LINQ")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
Button Awal
Try
NilAwal = 0
DgView.DataSource = AccessSupplier.GetSupplierRows(NilAwal, NilPage)
LabelRecord()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Dan button Akhir
Try
NilTotal = AccessSupplier.GetTotalBaris
NilBagi = NilTotal Mod NilPage
NilAwal = NilTotal - IIf(NilBagi <> 0, NilBagi, NilPage)
DgView.DataSource = AccessSupplier.GetSupplierRows(NilAwal, NilPage)
LabelRecord()
Catch ex As Exception
MsgBox(ex.Message)
End Try

Tidak ada komentar:

Posting Komentar