Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

GridView - How to filter dates by the month

$
0
0

By default, GridView uses cell values for filtering. At the same time, the control can display custom text in cells. The solution is based on these aspects.

1. Add an unbound columns to display dates:

[C#]
settings.Columns.Add(column=>{   column.FieldName="Month";   column.ColumnType=MVCxGridViewColumnType.DateEdit;   column.UnboundType=DevExpress.Data.UnboundColumnType.Integer;   column.SettingsHeaderFilter.Mode=GridHeaderFilterMode.CheckedList;})

 Note, its UnboundType is Integer, but not DateTime. The column will contain the numbers of months.

Handle the CustomUnboundColumnData event to populate column cells by month numbers:

[C#]
settings.CustomUnboundColumnData=(sender,e)=>{   if(e.Column.FieldName=="Month")   {       DateTimevalue=(DateTime)e.GetListSourceFieldValue("C5");       e.Value=value.Month;   }};

 The C5 field is a real column in GridView. If you do not need to show it to an end-user, you can hide this column. However, it is required for getting unbound column data.


Now the unbound column displays month values. However, if you wish to show an end-user full dates, handle the CustomColumnDisplayText event and implement the following handler:

[C#]
settings.CustomColumnDisplayText=(sender,e)=>{   if(e.Column.FieldName=="Month")   {       DateTimerealValue=((DateTime)e.GetFieldValue("C5"));       e.DisplayText=realValue.ToString("dd MMM yyyy");   }};

 The data is returned from the bound C5 data column, but shown in the unbound column.


Finally, update the Header Filter items as demonstrated below:

[C#]
settings.HeaderFilterFillItems=(sender,e)=>{   if(e.Column.FieldName=="Month")   {       e.Values.Clear();       e.AddValue("January","1");       e.AddValue("February","2");       e.AddValue("March","3");       e.AddValue("April","4");       e.AddValue("May","5");       e.AddValue("June","6");       e.AddValue("July","7");       e.AddValue("August","8");       e.AddValue("September","9");       e.AddValue("October","10");       e.AddValue("November","11");       e.AddValue("December","12");   }};

Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>