Locations of visitors to this page


Paging a Windows Forms DataGridView with LINQ

Onteora Software

Ken Tucker's Blog

About the author

Author Name is someone.
E-mail me Send mail

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Paging a Windows Forms DataGridView with LINQ

Since Visual Studio 2008 is due out by the end of the Month I am updating some of my datagridview samples for LINQ.

To start off create a new windows forms application in VS 2008 make sure you select FrameWork 3.5 so you can use linq

 

 

 

I now added a new Linq to Sql designer to the project and named it Northwind.  Drag the Northwind Products Table on to the design surface from the Server Explorer.

 

 

 

 

 

On your windows forms add a DataGridView (DataGridView1) and a NumericUpDown control (nuPage).  For this example I will have the datagridview show 15 items at a time.  In the form load event we will figure out how many pages there are and load the first 15 items into the datagridview.  In the NumericUpdown controls value changed event we will update the data displayed

 

Public Class Form1
    Dim bs As New BindingSource

    Private intPages As Integer
    Dim db As New NorthwindDataContext

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        intPages = Math.Ceiling(db.Products.Count / 15)
        nuPage.Maximum = intPages
        nuPage.Minimum = 1
        Dim p = From prod In db.Products _
                Select prod Skip 0 Take 15
        bs.DataSource = p
        bs.AllowNew = False
        DataGridView1.DataSource = bs
    End Sub


    Private Sub nuPage_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles nuPage.ValueChanged
        Dim p = From prod In db.Products _
                Select prod Skip (nuPage.Value - 1) * 15 Take 15
        bs.DataSource = p
    End Sub
End Class

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: DataGridView | Linq | VS 2008
Posted by Ken Tucker on Tuesday, November 06, 2007 2:43 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Wednesday, November 19, 2008 12:20 PM