paging with gridview

M

Marc Grutte

Hi

I am trying to bind a custom datasource to a gridview whilst paging is
enabled. What is missing in this code to make the paging + binding work?

thanks

M

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Public Class person

Dim _name As String

Public Property Name()

Get

Return _name

End Get

Set(ByVal value)

_name = value

End Set

End Property





End Class

Public Class PersonCollection : Inherits CollectionBase

Sub New()

MyBase.New()

End Sub

Public Property item(ByVal i As Integer)

Get

Return Me.List(i)

End Get

Set(ByVal value)

Me.List(i) = value

End Set

End Property

Public Sub add(ByVal item As person)

Me.List.Add(item)

End Sub

End Class

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load

If Not IsPostBack Then

GridView2.DataSource = bron()

GridView2.DataBind()

End If


End Sub

Protected Function bron() As PersonCollection

Dim objPersonCollection As New PersonCollection

For a As Integer = 0 To 3

Dim objPerson As New person

objPerson.Name = "Name" + a.ToString

objPersonCollection.add(objPerson)

Next

Return objPersonCollection

End Function

Protected Sub GridView2_PageIndexChanging(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewPageEventArgs) Handles
GridView2.PageIndexChanging





End Sub

Protected Sub GridView2_PageIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles GridView2.PageIndexChanged





End Sub







</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:GridView ID="GridView2" runat="server" AllowPaging="True" PageSize="2"
AutoGenerateColumns="False">

<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:RadioButtonList ID="rblCategories" SelectedValue="<%#Bind('Name') %>"
runat="server">

<asp:ListItem Text="John" Value="Name0"></asp:ListItem>

<asp:ListItem Text="Nick" Value="Name1"></asp:ListItem>

<asp:ListItem Text="Bob" Value="Name2"></asp:ListItem>

<asp:ListItem Text="Ed" Value="Name3"></asp:ListItem>

</asp:RadioButtonList></ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

</div>

</form>

</body>

</html>
 
G

Guest

Marc,

When you bind the GridView to a datasource that is not a DataSourceControl
(something other than SqlDataSource, ObjectDataSource, etc.) then you lose
some of the automatic functionality like paging and sorting.

You can bind the GridView to a class that implements ICollection but then
you'll have to handle some of the plumbing yourself.

Try modifying your PageIndexChanging handler to:

Protected Sub GridView2_PageIndexChanging(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles
GridView2.PageIndexChanging
Me.GridView2.PageIndex = e.NewPageIndex
GridView2.DataSource = bron()
GridView2.DataBind()
End Sub

If handling the events manually is a problem then you might take a look at
using an ObjectDataSource to expose your collection.
http://webproject.scottgu.com/CSharp/Data/Data.aspx

This one has some good details about handling sorting
http://msdn2.microsoft.com/en-us/library/aa479347.aspx

hope this helps,
Jason Vermillion
 
G

Guest

thanks Jason!

Jason Vermillion said:
Marc,

When you bind the GridView to a datasource that is not a DataSourceControl
(something other than SqlDataSource, ObjectDataSource, etc.) then you lose
some of the automatic functionality like paging and sorting.

You can bind the GridView to a class that implements ICollection but then
you'll have to handle some of the plumbing yourself.

Try modifying your PageIndexChanging handler to:

Protected Sub GridView2_PageIndexChanging(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles
GridView2.PageIndexChanging
Me.GridView2.PageIndex = e.NewPageIndex
GridView2.DataSource = bron()
GridView2.DataBind()
End Sub

If handling the events manually is a problem then you might take a look at
using an ObjectDataSource to expose your collection.
http://webproject.scottgu.com/CSharp/Data/Data.aspx

This one has some good details about handling sorting
http://msdn2.microsoft.com/en-us/library/aa479347.aspx

hope this helps,
Jason Vermillion
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top