Reputation: 121
Hi i am using telerik radgrid. I am adding DataSource and Rebinding it in code behind in cs file.
Problem is filtering is not working on radgrid.
Any help will be much appreciated.
Thanks
<telerik:RadGrid ID="ShopOrderRadGrid" runat="server" GridLines="None" OnItemDataBound="ShopOrderRedGridRadGrid__onItemDataBound" OnItemCommand="ShopOrderRedGridRadGrid_ItemCommand" OnNeedDataSource="ShopOrderRadGrid_NeedDataSource" AllowFilteringByColumn="True" AllowSorting="True" AutoGenerateColumns="False" AllowPaging="True"
PageSize="100">
<MasterTableView AutoGenerateColumns="False" DataKeyNames="ShopOrderId" HierarchyDefaultExpanded="True" CommandItemDisplay="Top" AllowSorting="True" AllowFilteringByColumn="True" AllowCustomSorting="True" ShowFooter="True">
<Columns>
<telerik:GridEditCommandColumn ButtonType="ImageButton" Reorderable="False" Resizable="False"
UniqueName="EditColumn" ShowSortIcon="False">
<HeaderStyle Width="30px" Wrap="False" />
<ItemStyle Width="30px" />
</telerik:GridEditCommandColumn>
<telerik:GridTemplateColumn HeaderText="Print" AllowFiltering="False">
<ItemTemplate>
<asp:Button runat="server" ID="PrintButton" CommandName="Print" Text="Print Order"
OnClientClick="openNewWindow();" />
</ItemTemplate>
<HeaderStyle Width="100px" Wrap="False" />
<ItemStyle Width="100px" />
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="SimpleCode" HeaderText="Order Code" DataType="System.String"
SortExpression="SimpleCode" UniqueName="SimpleCode"
AllowFiltering="True" ShowFilterIcon="False"
AutoPostBackOnFilter="True"
AndCurrentFilterFunction="Contains"
CurrentFilterFunction="Contains">
</telerik:GridBoundColumn>
<telerik:GridTemplateColumn HeaderText="Name" SortExpression="CustomerFirstname"
AllowFiltering="True" ShowFilterIcon="False"
AutoPostBackOnFilter="True"
AndCurrentFilterFunction="Contains"
CurrentFilterFunction="Contains">
<ItemTemplate>
<asp:Label ID="NameLabel" runat="server"></asp:Label>
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridBoundColumn DataField="TotalCostIncludingTax" HeaderText="Total Cost Including Tax"
SortExpression="TotalCostIncludingTax" ShowFilterIcon="False" UniqueName="TotalCostIncludingTax"
DataFormatString="{0:C}" />
</Columns>
</MasterTableView>
</telerik:RadGrid>
Here is a c# code for this file.
protected void Page_Load(object sender, EventArgs e)
{
try
{
shopId = new Guid(Session["shopId"].ToString());
}
catch (Exception)
{
Response.Redirect("~/Shop/Shop_list.aspx");
}
// checking while filtering
if (StartDate.SelectedDate == null)
{
StartDate.SelectedDate = DateTime.Now.Date.AddDays(-14);
}
if (FinishDate.SelectedDate == null)
{
FinishDate.SelectedDate = DateTime.Now.Date;
}
if (!IsPostBack)
{
FilterShopOrderData();
}
}
private void FilterShopOrderData()
{
IObjectScope scope = ORM.ScopeFactory.GetPerRequestScope(HttpContext.Current);
DateTime stdate = DateTime.Parse(StartDate.SelectedDate.ToString());
DateTime endate = DateTime.Parse(FinishDate.SelectedDate.ToString());
DateTime startOfDay = new DateTime(stdate.Year, stdate.Month, stdate.Day, 00, 00, 00);
DateTime endOfDay = new DateTime(endate.Year, endate.Month, endate.Day, 23, 59, 59);
//Rebind RadGrid Of session.
List<ORM.Shoporder> shopOrders = null;
if (CheckBoxShowOrderStatus.Checked == true)
{
shopOrders = (from shopOrder in scope.Extent<ORM.Shoporder>()
where shopOrder.ShopId == shopId && shopOrder.ShoporderStatus.IsComplete == !CheckBoxShowOrderStatus.Checked &&
shopOrder.CreateDateTime.Date >= startOfDay && shopOrder.CreateDateTime.Date <= endOfDay
&& shopOrder.IsDeleted == false
&& (shopOrder.IsReceiptSent == true || shopOrder.IsReceiptSkipped == true || shopOrder.IsPDFSent == true)
orderby shopOrder.CreateDateTime descending
select shopOrder).ToList();
SubjectHeadingLabel.Text = DAL.DataClasses.Shop.GetShopNameById(shopId) + ": Incomplete Orders";
}
else
{
shopOrders = (from shopOrder in scope.Extent<ORM.Shoporder>()
where shopOrder.ShopId == shopId &&
shopOrder.CreateDateTime.Date >= startOfDay && shopOrder.CreateDateTime.Date <= endOfDay
&& shopOrder.IsDeleted == false
&& (shopOrder.IsReceiptSent == true || shopOrder.IsReceiptSkipped == true || shopOrder.IsPDFSent == true)
orderby shopOrder.CreateDateTime descending
select shopOrder).ToList();
SubjectHeadingLabel.Text = DAL.DataClasses.Shop.GetShopNameById(shopId) + ": All Orders";
}
ShopOrderRadGrid.DataSource = shopOrders;
//ShopOrderRadGrid.DataBind();
ShopOrderRadGrid.Rebind();
}
protected void ShopOrderRedGridRadGrid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
GridDataItem dataItem = e.Item as GridDataItem;
if (e.CommandName == RadGrid.EditCommandName)
{
//Get the PK of the item.
Guid shopOrderId = new Guid();
shopOrderId = (Guid)dataItem.GetDataKeyValue("ShopOrderId");
//store PK in session
Session["shopOrderId"] = shopOrderId.ToString();
//redirect to the Order mod page.
Response.Redirect("~/Shop/Order/Order_mod.aspx");
}
if (e.CommandName == RadGrid.InitInsertCommandName)
{
//Get the PK of the item.
//clear the session
Session["shopOrderId"] = null;
//redirect to the Order mod page.
Response.Redirect("~/Shop/Order/Order_mod.aspx");
}
//For Delete
if (e.CommandName == "Delete")
{
Guid shopOrderId = Guid.Empty;
shopOrderId = (Guid)dataItem.GetDataKeyValue("ShopOrderId");
IObjectScope scope = ORM.ScopeFactory.GetPerRequestScope(HttpContext.Current);
scope.Transaction.Begin();
ORM.Shoporder shopOrder = DAL.DataClasses.ShopOrder.GetObjectById(shopOrderId);
shopOrder.IsDeleted = true;
scope.Transaction.Commit();
//ShopOrderRadGrid.Rebind();
FilterShopOrderData();
}
if (e.CommandName == "FilterRadGrid")
{
FilterShopOrderData();
}
}
protected void ShopOrderRedGridRadGrid__onItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
Guid shopOrderId = new Guid();
ImageButton button = item["DeleteColumn"].Controls[0] as ImageButton;
button.Attributes["onclick"] = "return confirm('Are you sure you want to delete this order? Deleted orders can be restored from Recycle Bin.')";
shopOrderId = new Guid(item.GetDataKeyValue("ShopOrderId").ToString());
ORM.Shoporder thisShopOrder = DAL.DataClasses.ShopOrder.GetObjectById(shopOrderId);
Label NameLabel = (Label)e.Item.FindControl("NameLabel");
NameLabel.Text = thisShopOrder.CustomerFirstname + " " + thisShopOrder.CustomerLastname;
ORM.Site thisSite = DAL.DataClasses.Company.GetDefaultSite(thisShopOrder.Shop.CompanyId);
Button PrintButton = (Button)item.FindControl("PrintButton");
PrintButton.OnClientClick = "openNewWindow('" + DAL.DataClasses.ShopOrder.GetOrderReceiptURL(shopOrderId) + "'); return false;";
Label OrderTypeLabel = (Label)e.Item.FindControl("OrderTypeLabel");
OrderTypeLabel.Text = "Physical Products";
if (thisShopOrder.IsEmailDelivery)
{
OrderTypeLabel.Text = "E-Products";
}
if (thisShopOrder.Shopdeliverymethod.IsPhysicalDelivery == true && thisShopOrder.IsEmailDelivery)
{
OrderTypeLabel.Text = "Physical & E-Products";
}
}
}
protected void ShopOrderRadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
// ShopOrderRadGrid.Rebind();
FilterShopOrderData();
}
Upvotes: 0
Views: 3750
Reputation: 2734
To apply a filter in the code behind you need 3 things:
Set a filter filter expression
RAD_Grid.MasterTableView.FilterExpression = "(Field >= DateTime.Today.AddDays(-14) )" ;
This is the Expression the grid will filter on.
Set your column current filter
This is Only for the filter display in your column header. This is just for display and future filter. This Must be set or the grid will just ignore any filter expression. You cannot filter on a data that is not in your column. You can filter even if the column is hidden.
GridColumn column = RAD_Grid.MasterTableView.GetColumnSafe("Field");
column.CurrentFilterFunction = GridKnownFunction.GreaterThanOrEqualTo ;
column.CurrentFilterValue = DateTime.Today.AddDays(-14).ToShortDateString();
Rebind
If you need a range or you have multiple filter concatenate/set them in the expression (1.)
If its an initial filter you can set it the aspx. this way no rebind needed.
EnableLinqExpressions="true" is something important writen in red in the documentation.
note :
If you are binding the grid using the NeedDataSource event, you can set the initial filter in the NeedDataSource event handler and omit the call to the Rebind method. The code for setting the filter must still be placed inside an if statement that checks that Page.IsPostBack is False . Note that this is applicable for non auto generated columns.
Upvotes: 1