)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to compare date columns using expression in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: April 27, 2016 |
Last Revised Date: December 17, 2019

Compare the date columns using expressions

To compare the date columns using expressions, can be handled by `SelectedIndexChanged` event.

1. FilterCompareOperator helps to give the comparer condition to filter the records.

2. RecordFilterDescriptor provides the filter criteria by displaying only a subset of records from the underlying data source.

C#:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    this.gridGroupingControl1.TableDescriptor.RecordFilters.Clear();
    string s = "";
    switch (comboBox1.SelectedIndex)
    {      
        case 0:  //dates >= April 17, 2008
            s = string.Format("[{0}]GreaterThanOrEqualTo {{{1}}}", "date", "4/17/2008");
            FilterCondition con = new FilterCondition(FilterCompareOperator.GreaterThanOrEqualTo, "4/17/2008");
            RecordFilterDescriptor rec = new RecordFilterDescriptor("date", con);
            this.gridGroupingControl1.TableDescriptor.RecordFilters.Add(rec);
            break;
        case 1: //dates < April 16, 2008
            s = string.Format("[{0}] LessThan {{{1}}}", "date", "4/16/2008");
            FilterCondition con1 = new FilterCondition(FilterCompareOperator.LessThan, "4/16/2008");
            RecordFilterDescriptor rec1 = new RecordFilterDescriptor("date", con1);
            this.gridGroupingControl1.TableDescriptor.RecordFilters.Add(rec1);
            break;
        case 2: //dates < April 19, 2008
            s = string.Format("[{0}] GreaterThan {{{1}}}", "date", "4/19/2008"); //first arg empty means DateTime.MinValue
            FilterCondition con2 = new FilterCondition(FilterCompareOperator.GreaterThan, "4/19/2008");
            RecordFilterDescriptor rec2 = new RecordFilterDescriptor("date", con2);
            this.gridGroupingControl1.TableDescriptor.RecordFilters.Add(rec2);
            break;
        default:
            break;
    }
     this.label2.Text = s;//so you can see the actual filter
}

 

VB:

Private Sub comboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboBox1.SelectedIndexChanged
 Me.gridGroupingControl1.TableDescriptor.RecordFilters.Clear()
 Dim s As String = ""
 Select Case comboBox1.SelectedIndex
  Case 0 'dates >= April 17, 2008
   s = String.Format("[{0}]GreaterThanOrEqualTo {{{1}}}", "date", "4/17/2008")
   Dim con As New FilterCondition(FilterCompareOperator.GreaterThanOrEqualTo, "4/17/2008")
   Dim rec As New RecordFilterDescriptor("date", con)
   Me.gridGroupingControl1.TableDescriptor.RecordFilters.Add(rec)
  Case 1 'dates < April 16, 2008
   s = String.Format("[{0}] LessThan {{{1}}}", "date", "4/16/2008")
   Dim con1 As New FilterCondition(FilterCompareOperator.LessThan, "4/16/2008")
   Dim rec1 As New RecordFilterDescriptor("date", con1)
   Me.gridGroupingControl1.TableDescriptor.RecordFilters.Add(rec1)
  Case 2 'dates < April 19, 2008
   s = String.Format("[{0}] GreaterThan {{{1}}}", "date", "4/19/2008") 'first arg empty means DateTime.MinValue
   Dim con2 As New FilterCondition(FilterCompareOperator.GreaterThan, "4/19/2008")
   Dim rec2 As New RecordFilterDescriptor("date", con2)
   Me.gridGroupingControl1.TableDescriptor.RecordFilters.Add(rec2)
  Case Else
 End Select
 Me.label2.Text = s 'so you can see the actual filter
End Sub

 

Compare the date column in gridcontrol

Compare the date column in gridcontrol

 

Samples:

C#: Compare Date columns

VB: Compare Date columns

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon