Codebanking.com Bank your code here...
HOME REGISTER TECHNICAL Q&A GENERAL Q&A SUBMIT CODE SNIPPET LOGIN
 
 Topics
 Tech Feeds
 Tech Fun
How to do paging, Sorting and Filtering in SPGridview?

How to do paging, Sorting and Filtering in SPGridview?

Sharepoint has a built-in gridview control, i.e. SPGridView control. It has built-in support for sorting, filtering functionalities when used with an ObjectDataSource, and the look and feel is similar if SPGridView control is used. This is the major reason behind SPGridView usage over ASP.NET GridView control.

Create class file which act as wrapper to maintain datatable

using System.Data;

namespace FWebpart
{
public class DataTableWrapper
{
private DataTable _dt = new DataTable();

public DataTableWrapper(DataTable dt)
{
_dt = dt;
}

public DataTable GetTable()
{
return _dt;
}
}
}


////////////////////////////////////////////////////////////////////////////

This is our webpart class file

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Collections;
using System.Web;
using System.Data;
using System.Text;
using System.Collections.Generic;

namespace Fwebpart
{
[Guid("b9416274-83e5-4d4c-9e92-e3ef00c99da9")]
public class FlexiReportWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{

private SPSite currentSite;
private DataTable dtResult;
private DataTableWrapper myDataTable;
private SPGridView spFR;
Button btnFilter;

 

protected override void CreateChildControls()
{

base.CreateChildControls();

try
{
dtResult = new DataTable();

if (ViewState["result"] != null)
{
dtResult = (DataTable)ViewState["result"];

}

myDataTable = new DataTableWrapper(dtResult);
Type t = myDataTable.GetType();


//objectDatasource
ObjectDataSource ds = new ObjectDataSource();
ds.ID = "myDataSource";
ds.TypeName = t.AssemblyQualifiedName;
ds.SelectMethod = "GetTable";
ds.ObjectCreating += new ObjectDataSourceObjectEventHandler(ds_ObjectCreating);
this.Controls.Add(ds);
spFR = new SPGridView();

spFR.ID = "FlexiGrid";
spFR.AutoGenerateColumns = false;
spFR.AllowSorting = true;
spFR.AllowFiltering = true;
spFR.DataSourceID = "myDataSource";
this.controls.add(spFR);
spFR.PageSize = 17;
spFR.AllowPaging = true;
spFR.PageIndexChanging += new GridViewPageEventHandler(spFR_PageIndexChanging);
spFR.PagerTemplate = null;

if (ViewState["result"] == null)
{
dtResult = SelectData();
myDataTable = new DataTableWrapper(dtResult);
}

spFR.DataBind();
}
catch (SPException ex)
{
displayError(ex.Message);
}

}

void ds_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
myDataTable = new DataTableWrapper(dtResult);
e.ObjectInstance = myDataTable;
}

 

void spFR_PageIndexChanging(object sender, GridViewPageEventArgs e)
{

spFR.PageIndex = e.NewPageIndex;
spFR.DataBind();
}

public DataTable SelectData()
{
try{

Create boundfield according ot the datatable coulmns...
BoundField column = new BoundField();
column.DataField = // Datatable ColumnName;
column.SortExpression = // Datatable ColumnName;
column.HtmlEncode = false;
spFR.Columns.Add(column);
spFR.FilterDataFields = // Datatable ColumnName;
spFR.FilteredDataSourcePropertyName = "FilterExpression";
spFR.FilteredDataSourcePropertyFormat = "{1} = ''{0}''";
ViewState["result"] = dtResult;
return dtResult;
}

catch (SPException ex)
{
displayError(ex.Message);
}
}

protected void btnFilter_Click(object sender, EventArgs e)
{
On button click binding the spgriview
spFR.Columns.Clear();
SelectData();
spFR.DataBind();
}

}

}


Comments Posted:
No Comments Posted
Please login for leave your comments
 
 2010 codebanking.com      contactus@codebanking.com