1. Add Below Code To Your .ascx File
<SharePoint:SPGridView ID="spgvOrder" runat="server" AutoGenerateColumns="false"
valign="top" AllowPaging="true" AllowSorting="true" AllowFiltering="true" FilterDataFields="Title,FirstName,LastName,State,Marks"
FilteredDataSourcePropertyName="FilterExpression" FilteredDataSourcePropertyFormat="{1} like '{0}'"
OnSorting="spgvOrder_Sorting">
<Columns>
<SharePoint:SPBoundField DataField="Title" HeaderText="Code" ItemStyle-HorizontalAlign="Left" />
<SharePoint:SPBoundField DataField="FirstName" HeaderText="First Name" ItemStyle-HorizontalAlign="Left" />
<SharePoint:SPBoundField DataField="LastName" HeaderText="Last Name" ItemStyle-HorizontalAlign="Left" />
<SharePoint:SPBoundField DataField="State" HeaderText="State" ItemStyle-HorizontalAlign="Left" />
<SharePoint:SPBoundField DataField="Marks" HeaderText="Marks" ItemStyle-HorizontalAlign="Left" />
</Columns>
</SharePoint:SPGridView>
<asp:ObjectDataSource runat="server" ID="gridds" OnFiltering="gridds_Filtering" OnObjectCreating="gridds_ObjectCreating" />
<SharePoint:SPGridViewPager ID="SPGridViewPager1" GridViewId="spgvOrder" runat="server"></SharePoint:SPGridViewPager>
2. Add Below Code To Bind List Items To SPGridView With Sorting & Filtering
protected void Page_Load(object sender, EventArgs e)
{
gridds.TypeName = this.GetType().AssemblyQualifiedName;
spgvOrder.DataSourceID = gridds.ID;
gridds.SelectMethod = "LoadData";
spgvOrder.PageSize = 50;
spgvOrder.DataBind();
}
protected void spgvOrder_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["FilterExpression"] != null)
{
gridds.FilterExpression = (string)ViewState["FilterExpression"];
}
}
protected void gridds_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)
{
ViewState["FilterExpression"] = ((ObjectDataSourceView)sender).FilterExpression;
}
protected sealed override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (Context.Request.Form["__EVENTARGUMENT"] != null &&
Context.Request.Form["__EVENTARGUMENT"].EndsWith("__ClearFilter__"))
{
// Clear FilterExpression
ViewState.Remove("FilterExpression");
}
}
protected void gridds_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = this;
}
public DataTable LoadData()
{
DataTable dt = null;
try
{
dt = new DataTable();
dt.Columns.Add("Title", typeof(string));
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));
dt.Columns.Add("State", typeof(string));
dt.Columns.Add("Marks", typeof(Int16));
using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb web = site.OpenWeb())
{
SPList lst = web.Lists["ListName"];
SPListItemCollection itmcol = lst.Items;
if (itmcol != null && itmcol.Count > 0)
{
DataRow dr = null;
foreach (SPListItem itm in itmcol)
{
dr = dt.NewRow();
dr["Title"] = itm["Title"];
dr["FirstName"] = itm["FirstName"];
dr["LastName"] = itm["LastName"];
dr["State"] = itm["State"];
dr["Marks"] = itm["Marks"];
dt.Rows.Add(dr);
}
}
}
}
}
catch (Exception ex)
{
throw ex.InnerException;
}
return dt;
}