In my Project i have a Grid which has some columns .Some of them are Numeric , but I need enable filtering on this Column.when I Enable ,I find out There is No 'Contains' item in the Filter List.I need to do it.I need Contains for NumericColumns .Is there any way To Enable 'Contains' for this Kind of Columns Or if not, Can I write some codes For This scenario and How?
18 Answers, 1 is accepted
Please try with the below code snippet.
<
telerik:GridBoundColumn
DataField
=
"ID"
UniqueName
=
"ID"
HeaderText
=
"ID"
DataType
=
"System.String"
></
telerik:GridBoundColumn
>
DataType="system.String"
May be after setting this DataType it might be possible that sorting is not working perfectly.
Thanks,
Jayesh Goyani
Thanks for your Reply.when I use DataType="System.String" , It shows 'Contains' and other Items But This Contains DOES NOT work on NumericColumns. Nothing Happens after Clicking on it.
On my side the suggested approach works exactly as requested:
<
telerik:GridBoundColumn
...
DataType
=
"System.String"
>
Alternatively, you can use the following configuration to achieve the requested functionality:
<
telerik:GridTemplateColumn
UniqueName
=
"NumericFieldName"
HeaderText
=
"Header"
DataField
=
"Freight"
DataType
=
"System.String"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
Text='<%# Eval("Freight") %>'></
asp:Label
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
Hope this helps. Please give it a try and let me know if it works for you.
Regards,
Eyup
Telerik
Thank you for Reply
When I Use DataType="System.string" ,It shows me Some Items Like 'Contains','StartWith','EndWith' ,.... .It Shows All Of items BUT this Items Doesn't Work for Integer.I mean When I click On This Items Nothing Happens.just Items like 'GreaterThan','LessThan','EqualTo' ,... Work for Integer . I tried Your Code But it acts Like this.
It is My Code :
<telerik:GridNumericColumn CurrentFilterFunction="equalto" DataField="ContractNumber"
FilterControlWidth="100px" HeaderStyle-Width="300px" UniqueName="ContractNumber"
HeaderText="شماره قرارداد" ShowFilterIcon="true">
<HeaderStyle Width="300px" />
</telerik:GridNumericColumn>I Changed GridNumericColumn to GridBoundColumn and GridTemplateColumn, But My Problem Exists yet.
I don't Know What My Problem is.
I have prepared a sample RadGrid web site to demonstrate that the suggested approach works as expected. Can you please run the attached application and try to determine the crucial differences between our projects?
Looking forward to your reply.
Regards,
Eyup
Telerik
Hi Eyup,
How can I filter Integer GridBoundColumn as string when EnableLinqExpressions="false" ? Your example didn't work...
Thank you
Vasssek
You can set the DataType="System.String" for the BoundColumn that contains a NumericField. Please provide your code snippet if this doesn't help.
ASPX:
<
telerik:GridBoundColumn
UniqueName
=
"Number"
DataType
=
"System.String"
DataField
=
"Number"
HeaderText
=
"Number"
>
</
telerik:GridBoundColumn
>
Thanks,
Shinu
Hi Shinu,
I have tried above example from Eyup, where I added EnableLinqExpressions="false" parameter. It didn't work either. It shows all rows instead of filtered ...
My DLLs version: 2013.3.1126.40
Here is the ASPX declaration:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadGridFilterNumericAsString.aspx.cs" Inherits="RadGridFilterNumericAsString" %>
<!DOCTYPE html>
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
<
title
></
title
>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
telerik:RadScriptManager
ID
=
"RadScriptManager1"
runat
=
"server"
>
<
Scripts
>
<
asp:ScriptReference
Assembly
=
"Telerik.Web.UI"
Name
=
"Telerik.Web.UI.Common.Core.js"
/>
<
asp:ScriptReference
Assembly
=
"Telerik.Web.UI"
Name
=
"Telerik.Web.UI.Common.jQuery.js"
/>
<
asp:ScriptReference
Assembly
=
"Telerik.Web.UI"
Name
=
"Telerik.Web.UI.Common.jQueryInclude.js"
/>
</
Scripts
>
</
telerik:RadScriptManager
>
<
script
type
=
"text/javascript"
>
//Put your JavaScript code here.
</
script
>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
=
"server"
>
</
telerik:RadAjaxManager
>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AllowPaging
=
"True"
CellSpacing
=
"0"
EnableLinqExpressions
=
"false"
DataSourceID
=
"SqlDataSource1"
GridLines
=
"None"
Width
=
"1300px"
AllowFilteringByColumn
=
"true"
>
<
MasterTableView
DataSourceID
=
"SqlDataSource1"
AutoGenerateColumns
=
"False"
DataKeyNames
=
"OrderID"
>
<
CommandItemSettings
ExportToPdfText
=
"Export to PDF"
></
CommandItemSettings
>
<
RowIndicatorColumn
Visible
=
"True"
FilterControlAltText
=
"Filter RowIndicator column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
RowIndicatorColumn
>
<
ExpandCollapseColumn
Visible
=
"True"
FilterControlAltText
=
"Filter ExpandColumn column"
>
<
HeaderStyle
Width
=
"20px"
></
HeaderStyle
>
</
ExpandCollapseColumn
>
<
Columns
>
<
telerik:GridBoundColumn
DataField
=
"OrderID"
DataType
=
"System.Int32"
FilterControlAltText
=
"Filter OrderID column"
HeaderText
=
"OrderID"
ReadOnly
=
"True"
SortExpression
=
"OrderID"
UniqueName
=
"OrderID"
>
</
telerik:GridBoundColumn
>
<
telerik:GridDateTimeColumn
DataField
=
"OrderDate"
DataType
=
"System.DateTime"
FilterControlAltText
=
"Filter OrderDate column"
HeaderText
=
"OrderDate"
SortExpression
=
"OrderDate"
UniqueName
=
"OrderDate"
>
</
telerik:GridDateTimeColumn
>
<
telerik:GridNumericColumn
DataField
=
"Freight"
DataType
=
"System.Decimal"
FilterControlAltText
=
"Filter Freight column"
HeaderText
=
"Filtered As Numeric"
SortExpression
=
"Freight"
UniqueName
=
"Freight1"
>
</
telerik:GridNumericColumn
>
<
telerik:GridBoundColumn
DataField
=
"Freight"
DataType
=
"System.String"
FilterControlAltText
=
"Filter Freight column"
HeaderText
=
"Filtered As String"
SortExpression
=
"Freight"
UniqueName
=
"Freight2"
>
</
telerik:GridBoundColumn
>
<
telerik:GridTemplateColumn
UniqueName
=
"Freight3"
HeaderText
=
"Filtered As String 2"
DataField
=
"Freight"
DataType
=
"System.String"
>
<
ItemTemplate
>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
Text='<%# Eval("Freight") %>'></
asp:Label
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridBoundColumn
DataField
=
"ShipName"
FilterControlAltText
=
"Filter ShipName column"
HeaderText
=
"ShipName"
SortExpression
=
"ShipName"
UniqueName
=
"ShipName"
>
</
telerik:GridBoundColumn
>
<
telerik:GridBoundColumn
DataField
=
"ShipCountry"
FilterControlAltText
=
"Filter ShipCountry column"
HeaderText
=
"ShipCountry"
SortExpression
=
"ShipCountry"
UniqueName
=
"ShipCountry"
>
</
telerik:GridBoundColumn
>
</
Columns
>
<
EditFormSettings
>
<
EditColumn
FilterControlAltText
=
"Filter EditCommandColumn column"
>
</
EditColumn
>
</
EditFormSettings
>
</
MasterTableView
>
<
FilterMenu
EnableImageSprites
=
"False"
>
</
FilterMenu
>
</
telerik:RadGrid
>
<
asp:SqlDataSource
ID
=
"SqlDataSource1"
runat
=
"server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [OrderID], [OrderDate], [Freight], [ShipName], [ShipCountry] FROM [Orders]"></
asp:SqlDataSource
>
</
form
>
</
body
>
</
html
>
Please help me to solve this issue.
Best regards.
Vasssek
I guess you want to apply Default Filter on Initial Load, hence you are setting EnableLinqExpressions="false". With EnableLinqExpressions="false",your required scenario cannot be obtained. You can set EnableLinqExpressions="true" and try the FilterExpression for default filter as mentioned here . Coding this way you can filter Integer GridBoundColumn as string. Below is a sample I have tried:
C#:
protected
void
RadGrid1_PreRender(
object
sender, EventArgs e)
{
//When EnableLinqExpressions="true", the FilterExpression is as follows
if
(!Page.IsPostBack)
{
RadGrid1.MasterTableView.FilterExpression = @
"it["
"ShipCountry"
"].ToString().Contains("
"Germany"
")"
;
GridColumn column = RadGrid1.MasterTableView.GetColumnSafe(
"ShipCountry"
);
column.CurrentFilterFunction = GridKnownFunction.Contains;
column.CurrentFilterValue =
"Germany"
;
RadGrid1.MasterTableView.Rebind();
}
}
Thanks,
Shinu
Hi Shinu,
you're right. I use it in RadGrid1_PreRender where I need to traverse all pages in the RadGrid until the newly inserted item is found. The problem is that I have approx. 100000 records and standard steps (Radgrid.rebind and select next page) are taken too much time.
Now, I do it this way:
string
filterExpression = RadGrid1.MasterTableView.FilterExpression.Length > 0 ? RadGrid1.MasterTableView.FilterExpression :
string
.Empty;
string
sortExpression = RadGrid1.MasterTableView.SortExpressions.Count > 0 ? RadGrid1.MasterTableView.SortExpressions[0].ToString() :
string
.Empty;
int
pageSize = RadGrid1.PageSize;
int
currentPageIndex = GetcurrentPageIndex(recordIDexpression, filterExpression, sortExpression, pageSize, (DataTable)RadGrid1.DataSource);
private
int
GetcurrentPageIndex(
string
recordIDexpression,
string
filterExpression,
string
sortExpression,
int
pageSize, DataTable dataTable)
{
DataRow[] foundrows =
null
;
DataRow[] foundrow =
null
;
DataTable custom_dt;
int
currentPageIndex = -1;
try
{
if
(dataTable.Rows.Count > 0)
{
if
(sortExpression.Length > 0)
{
dataTable.DefaultView.Sort = sortExpression;
dataTable = dataTable.DefaultView.ToTable();
}
if
(filterExpression.Length > 0)
{
foundrows = dataTable.Select(filterExpression);
if
(foundrows.Length > 0)
{
custom_dt = foundrows.CopyToDataTable();
foundrow = custom_dt.Select(recordIDexpression);
currentPageIndex = custom_dt.Rows.IndexOf(foundrow[0]) / pageSize;
}
}
else
{
foundrow = dataTable.Select(recordIDexpression);
currentPageIndex = dataTable.Rows.IndexOf(foundrow[0]) / pageSize;
}
}
}
catch
(Exception ex)
{
}
return
currentPageIndex;
}
As you can see function GetcurrentPageIndex uses datatable.select with SQL filter expression (EnableLinqExpressions="false").
If I want to use numeric column filtering as string I must use EnableLinqExpressions="true" and rewrite datatable select and sort via linq language or do you have any idea ?
Best regards
Vasssek
To be able to filter numeric field as a string one, you need to keep EnableLinqExpressions set as True. In case you want to navigate to the newly inserted item, you can use the approach demonstrated in the attached sample once you have the item's unique ID.
Hope this helps.
Regards,
Eyup
Telerik
your solution is nice, but can be used just in case when there isn't set any sortExpression or filterExpression in Radgrid. It seems that your function GetRecordPosition didn't accept this possibility...
Please, can you provide linq example even with sort and filter expression ?
P.S. It would be perfect, if something like this exist even for Radgrid:
http://decisivedata.net/server-side-paging-sorting-and-filtering-using-the-kendo-ui-grid-and-entity-framework-code-first/
Best regards
Vasssek
Yes, this is correct because the GetRecordPosition method returns the original record index from the datasource, but in the grid it has a different position after the Sort or Filter expression applied.
In that case, I suggest you to use the double Rebind() approach demonstrated in this sample:
http://www.telerik.com/community/code-library/aspnet-ajax/grid/get-selected-items-through-all-pages.aspx
Thank you for the suggestion. You can log it in our feedback portal so our developers take it under consideration:
http://feedback.telerik.com/Project/108
Regards,
Eyup
Telerik
i want to use "contains" on numeric field.
The "contains" operator cannot be used on numeric fields. In case you need to perform such operations, I would suggest using a string field that holds the numbers, however that would cause issues with the sorting and would. That approach would also require a custom editor template which manually initializes a NumericTextBox widget, otherwise the fields would be edited in a regular textbox (since the field type is string).
Regards,
Alexander Popov
Telerik
In other issue, i have a kendo dropdown list that i can't bind an Enum to.
Every prop in the Enum have a description that i would like to display and a value. I use vb.net with mvc 3
Here is an example:
public
class
ProductViewModel
{
public
int
ProductID
{
get
;
set
; }
public
string
ProductName
{
get
;
set
; }
}
Now, if we have a Grid showing the IDs and the Product names, you can use the Contains filter on the ProductName but not on the ProductID. If, for example, the ProductID is of type string, then you would be able to use the Contains operator.
The issue with the DropDownList however is not related to this topic and I would ask you to open a new support ticket for it.
Regards,
Alexander Popov
Telerik
columns.Bound(Function(p) p.PriorityEndDateTime).ClientTemplate("#=kendo.toString(PriorityEndDateTime,'yyyy/MM/dd')#").
HeaderTemplate("End Date").
HeaderHtmlAttributes(New With {.class = "col-xs-2"})
i understand it is not possible to filter on numeric columns such as dates so i thought to create a hidden column that will hold the date values as strings, the only problem is that i don't know how to convert those columns into strings.
please show me how.
best regards