Category / Section
How to customize the date format in filter bar of DynamicFilter in WinForms GridGroupingControl?
2 mins read
Customize the date format in filter bar of dynamic filter
In order to customize the date format in the filterbar cell, the Format property of the FilterBarCell property, FilterBarSelectedItemChanged, and TableControlDrawCellDisplayText events can be used.
C#
//Customize the DateFormat.
this.gridGroupingControl1.TableDescriptor.Columns["Date"].Appearance.FilterBarCell.Format = "dd-MMM-yyyy";
this.gridGroupingControl1.TopLevelGroupOptions.ShowFilterBar = true;
for (int i = 0; i < this.gridGroupingControl1.TableDescriptor.Columns.Count; i++)
{
this.gridGroupingControl1.TableDescriptor.Columns[i].AllowFilter = true;
}
GridDynamicFilter dynamicFilter = new GridDynamicFilter();
dynamicFilter.WireGrid(this.gridGroupingControl1);
//Triggering the FilterBarSelectedItemChanged event.
this.gridGroupingControl1.FilterBarSelectedItemChanged += gridGroupingControl1_FilterBarSelectedItemChanged;
//Triggering the TableControlDrawCellDisplayText event.
this.gridGroupingControl1.TableControlDrawCellDisplayText += gridGroupingControl1_TableControlDrawCellDisplayText;
//Handling the TableControlDrawCellDisplayText event.
void gridGroupingControl1_TableControlDrawCellDisplayText(object sender, GridTableControlDrawCellDisplayTextEventArgs e)
{
GridTableCellStyleInfo style = (GridTableCellStyleInfo)e.Inner.Style;
if (style.TableCellIdentity.Column != null && style.TableCellIdentity.Column.Name == "Date" && style.TableCellIdentity.TableCellType == GridTableCellType.FilterBarCell)
{
if (style.Text != "(All)" && style.Text != "(Custom....)" && style.Text != "(Empty)")
e.Inner.DisplayText = string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(style.Text));
}
}
//Handling the FilterBarSelectedItemChanged event.
void gridGroupingControl1_FilterBarSelectedItemChanged(object sender, FilterBarSelectedItemChangedEventArgs e)
{
if (e.Column != null && e.Column.Name == "Date")
{
int row = this.gridGroupingControl1.TableControl.CurrentCell.RowIndex;
int col = this.gridGroupingControl1.TableControl.CurrentCell.ColIndex;
GridTableCellStyleInfo style = this.gridGroupingControl1.TableControl.GetTableViewStyleInfo(row, col);
GridTableFilterBarExtCellRenderer renderer = this.gridGroupingControl1.TableControl.GetCellRenderer(row, col) as GridTableFilterBarExtCellRenderer;
renderer.ControlText = string.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(style.Text));
}
}
VB
'Customize the DateFormat.
Me.gridGroupingControl1.TableDescriptor.Columns("Date").Appearance.FilterBarCell.Format = "dd-MMM-yyyy"
Me.gridGroupingControl1.TopLevelGroupOptions.ShowFilterBar = True
For i As Integer = 0 To Me.gridGroupingControl1.TableDescriptor.Columns.Count - 1
Me.gridGroupingControl1.TableDescriptor.Columns(i).AllowFilter = True
Next i
Dim dynamicFilter As New GridDynamicFilter()
dynamicFilter.WireGrid(Me.gridGroupingControl1)
'Triggering the FilterBarSelectedItemChanged event.
AddHandler Me.gridGroupingControl1.FilterBarSelectedItemChanged, AddressOf gridGroupingControl1_FilterBarSelectedItemChanged
'Triggering the TableControlDrawCellDisplayText event.
AddHandler Me.gridGroupingControl1.TableControlDrawCellDisplayText, AddressOf gridGroupingControl1_TableControlDrawCellDisplayText
'Handling the TableControlDrawCellDisplayText event.
void gridGroupingControl1_TableControlDrawCellDisplayText(Object sender, GridTableControlDrawCellDisplayTextEventArgs e)
Dim style As GridTableCellStyleInfo = CType(e.Inner.Style, GridTableCellStyleInfo)
If style.TableCellIdentity.Column IsNot Nothing AndAlso style.TableCellIdentity.Column.Name = "Date" AndAlso style.TableCellIdentity.TableCellType = GridTableCellType.FilterBarCell Then
If style.Text <> "(All)" AndAlso style.Text <> "(Custom....)" AndAlso style.Text <> "(Empty)" Then
e.Inner.DisplayText = String.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(style.Text))
End If
End If
'Handling the FilterBarSelectedItemChanged event.
void gridGroupingControl1_FilterBarSelectedItemChanged(Object sender, FilterBarSelectedItemChangedEventArgs e)
If e.Column IsNot Nothing AndAlso e.Column.Name = "Date" Then
Dim row As Integer = Me.gridGroupingControl1.TableControl.CurrentCell.RowIndex
Dim col As Integer = Me.gridGroupingControl1.TableControl.CurrentCell.ColIndex
Dim style As GridTableCellStyleInfo = Me.gridGroupingControl1.TableControl.GetTableViewStyleInfo(row, col)
Dim renderer As GridTableFilterBarExtCellRenderer = TryCast(Me.gridGroupingControl1.TableControl.GetCellRenderer(row, col), GridTableFilterBarExtCellRenderer)
renderer.ControlText = String.Format("{0:dd-MMM-yyyy}", Convert.ToDateTime(style.Text))
End If
The screenshot below illustrates the formatting Filterbar cell in the GridGroupingControl.
Samples:
Reference Link: Filtering