Search code examples
c#datedatagridviewfilteringbindingsource

Filtering BindingSource between two dates discrepancy


I'm trying to filter a datagridview by dates using bindingsource. It is working but have some discrepancy when selecting dates. Please see image:

enter image description here

And if i select between 12-12-2014 AND 15-12-2014 the selection works fine. Here is my bindingsource code:

dgvEquipamentos.Columns["Data"].HeaderText.ToString() + " >= '" + dataInicial  + "' AND " +
dgvEquipamentos.Columns["Data"].HeaderText.ToString() + " <= '" + dataFinal + "' ";

Why is this happening? I have no idea and I couldn't find anything to help me. Maybe I'm searching wrong.

Any help would be appreciated, thanks.


Solution

  • I got it!! The problem was that my database field was DATETIME and the filtering was not working because of the timestamp. In my case the time was not really necessary so i change it to DATE only. For others that may need the timestamp check this solution:

    DateTime newfilter = Filter_AS2.Value;
    string filterstring = newfilter.ToString();
    
    this.VIEWBindingSource.Filter = "FROM_DATE <= #" + filterstring + 
    "# AND TO_DATE > #" + filterstring     + "#";
    

    See Font