Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Tuesday, January 15, 2013

Paging with SpGridView within a User Control




Ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SgvPagerSampleUserControl.ascx.cs" Inherits="SgvPagerSample.SgvPagerSample.SgvPagerSampleUserControl" %>
<SharePoint:SPGridView ID="sgvPagersample" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="Employee ID" DataField="Title" />
<asp:BoundField HeaderText="Employee Name" DataField="EmpName" />
<asp:BoundField HeaderText="Employee Age" DataField="EmpAge" />
</Columns>
</SharePoint:SPGridView>



Ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Security;

namespace SgvPagerSample.SgvPagerSample
{
    public partial class SgvPagerSampleUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            sgvPagersample.AllowPaging = true;
            sgvPagersample.PageSize = 3;
            sgvPagersample.PagerTemplate = null;
            sgvPagersample.PagerSettings.Mode = (PagerButtons)PagerMode.NextPrev;
            sgvPagersample.PagerSettings.NextPageText = "Next ";
            sgvPagersample.PagerSettings.PreviousPageText = "Previous";
            sgvPagersample.PageIndexChanging += new GridViewPageEventHandler(sgvPagersample_PageIndexChanging);
            if (!IsPostBack)
            {
                BindGridView();
            }
        }

        void sgvPagersample_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            sgvPagersample.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        public void BindGridView()
        {
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lstEmployee = currentWeb.Lists["Employee Details Search"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
            SPListItemCollection myColl = lstEmployee.GetItems(sQuery);
            if (myColl.Count > 0)
            {
                sgvPagersample.DataSource = myColl.GetDataTable();
                sgvPagersample.DataBind();
            }
        }
    }
}


No comments:

Post a Comment