Generate a SharePoint gridview with filtering, sorting , paging.
Define the SPGridview like
---------------------------
<SharePoint:SPGridView ID="gridTaskOrders" runat="server" AutoGenerateColumns="false" EnableModelValidation="true" GridLines="None" OnRowDataBound="gridTaskOrders_RowDataBound" AllowFiltering="true" FilterDataFields="Title,Client" FilteredDataSourcePropertyName="FilterExpression" AllowSorting="true" DataSourceID="odsGridTaskOrders" FilteredDataSourcePropertyFormat="{1} like '{0}'" PageSize="10" AllowPaging="true" OnSorting="gridTaskOrders_Sorting" >
<Columns>
<asp:TemplateField HeaderText="Task Order Name" HeaderStyle-CssClass="GridHeaderStyleText" ItemStyle-CssClass="GridItemStyle" SortExpression="Title">
<ItemTemplate>
<asp:Literal ID="ltrlTaskOrderTitle" runat="server" Text='<%#Eval("Title")%>'> </asp:Literal>
</ItemTemplate>
</asp:TemplateField>
<SharePoint:SPBoundField HeaderText="Client" DataField="Client" SortExpression="Client" />
</Columns>
</SharePoint:SPGridView>
Filtering and sorting are enabled on all the columns.
Define the ObjectDataSource for the SPGridview as below
-------------------------------------------------------
<asp:ObjectDataSource ID="odsGridTaskOrders" runat="server" SelectMethod="bindGrid"
onfiltering="odsGridTaskOrders_Filtering" onobjectcreating="odsGridTaskOrders_ObjectCreating">
</asp:ObjectDataSource>
Here bindGrid() is a method which returns DataTable to bind SPGridview
Define the SPGridviewPager as follows for paging
------------------------------------------------
<SharePoint:SPGridViewPager id="sgvPager" runat="Server" GridViewId="gridTaskOrders"></SharePoint:SPGridViewPager>
For bind data to Grid
---------------------
If we want to bind data to grid then use the below line of code.
gridTaskOrders.DataBind();
Sorting Code
-------------
private void gridTaskOrders_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["FilterExpression"] != null)
{
odsGridTaskOrders.FilterExpression = (string)ViewState["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");
}
}
Filtering code
--------------
protected void odsGridTaskOrders_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)
{
ViewState["FilterExpression"] = ((ObjectDataSourceView)sender).FilterExpression;
}
Object creating code
----------------------
//Used to deal with the ObjectCreated event
protected void odsGridTaskOrders_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = this;
}
RowDataBound event
------------------
protected void gridTaskOrders_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.Header)
{
if (!string.IsNullOrEmpty(gridTaskOrders.FilterFieldName))
{
for (int i = 0; i < gridTaskOrders.Columns.Count; i++)
{
DataControlField field = gridTaskOrders.Columns[i];
if (field.SortExpression == gridTaskOrders.FilterFieldName)
{
Image filterIcon = new Image();
filterIcon.ImageUrl = "/_layouts/images/filter.gif";
filterIcon.Style[HtmlTextWriterStyle.MarginLeft] = "2px";
Literal headerText = new Literal();
headerText.Text = field.HeaderText;
PlaceHolder panel = new PlaceHolder();
panel.Controls.Add(headerText);
panel.Controls.Add(filterIcon);
e.Row.Cells[i].Controls[0].Controls.Add(panel);
break;
}
}
}
}
}
}
Define the SPGridview like
---------------------------
<SharePoint:SPGridView ID="gridTaskOrders" runat="server" AutoGenerateColumns="false" EnableModelValidation="true" GridLines="None" OnRowDataBound="gridTaskOrders_RowDataBound" AllowFiltering="true" FilterDataFields="Title,Client" FilteredDataSourcePropertyName="FilterExpression" AllowSorting="true" DataSourceID="odsGridTaskOrders" FilteredDataSourcePropertyFormat="{1} like '{0}'" PageSize="10" AllowPaging="true" OnSorting="gridTaskOrders_Sorting" >
<Columns>
<asp:TemplateField HeaderText="Task Order Name" HeaderStyle-CssClass="GridHeaderStyleText" ItemStyle-CssClass="GridItemStyle" SortExpression="Title">
<ItemTemplate>
<asp:Literal ID="ltrlTaskOrderTitle" runat="server" Text='<%#Eval("Title")%>'> </asp:Literal>
</ItemTemplate>
</asp:TemplateField>
<SharePoint:SPBoundField HeaderText="Client" DataField="Client" SortExpression="Client" />
</Columns>
</SharePoint:SPGridView>
Filtering and sorting are enabled on all the columns.
Define the ObjectDataSource for the SPGridview as below
-------------------------------------------------------
<asp:ObjectDataSource ID="odsGridTaskOrders" runat="server" SelectMethod="bindGrid"
onfiltering="odsGridTaskOrders_Filtering" onobjectcreating="odsGridTaskOrders_ObjectCreating">
</asp:ObjectDataSource>
Here bindGrid() is a method which returns DataTable to bind SPGridview
Define the SPGridviewPager as follows for paging
------------------------------------------------
<SharePoint:SPGridViewPager id="sgvPager" runat="Server" GridViewId="gridTaskOrders"></SharePoint:SPGridViewPager>
For bind data to Grid
---------------------
If we want to bind data to grid then use the below line of code.
gridTaskOrders.DataBind();
Sorting Code
-------------
private void gridTaskOrders_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["FilterExpression"] != null)
{
odsGridTaskOrders.FilterExpression = (string)ViewState["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");
}
}
Filtering code
--------------
protected void odsGridTaskOrders_Filtering(object sender, ObjectDataSourceFilteringEventArgs e)
{
ViewState["FilterExpression"] = ((ObjectDataSourceView)sender).FilterExpression;
}
Object creating code
----------------------
//Used to deal with the ObjectCreated event
protected void odsGridTaskOrders_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = this;
}
RowDataBound event
------------------
protected void gridTaskOrders_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.Header)
{
if (!string.IsNullOrEmpty(gridTaskOrders.FilterFieldName))
{
for (int i = 0; i < gridTaskOrders.Columns.Count; i++)
{
DataControlField field = gridTaskOrders.Columns[i];
if (field.SortExpression == gridTaskOrders.FilterFieldName)
{
Image filterIcon = new Image();
filterIcon.ImageUrl = "/_layouts/images/filter.gif";
filterIcon.Style[HtmlTextWriterStyle.MarginLeft] = "2px";
Literal headerText = new Literal();
headerText.Text = field.HeaderText;
PlaceHolder panel = new PlaceHolder();
panel.Controls.Add(headerText);
panel.Controls.Add(filterIcon);
e.Row.Cells[i].Controls[0].Controls.Add(panel);
break;
}
}
}
}
}
}
Great post. Thanx for helping me out
ReplyDeleteCan you please help
ReplyDeleteA type must be specified in the TypeName property of ObjectDataSource 'odsGridTaskOrders'
GridView filtering in .net
ReplyDelete