简体   繁体   中英

How to use operators between table grid column and datetime in C#?

I have a grid on my form with column ExpirationDate . Above the grid I have two check boxes, active and inactive .

When I click active check box I want that my grid shows me only rows with expiration date greater or equal than today date.

When I set condition like this:

private string OR(string filter)
{
    if (filter != "")
        return "OR";

    return "";
}

private void chbFilter_CheckedChanged(object sender, EventArgs e)
{
    string filter = "";
    if (chbAktivan.Checked)
        filter += ExpirationDate >= DateTime.Today;
    if (chbNeaktivan.Checked)
    {
        filter += OR(filter);
        filter += ExpirationDate < DateTime.Today;
    }

    bsFilter.Filter = filter;
}

Please, focus just on my code filter += ExpirationDate >= DateTime.Today .

The error is:

Error 196 Operator '>=' cannot be applied to operands of type 'System.Windows.Forms.DataGridViewTextBoxColumn' and 'System.DateTime'

Any idea?

Just Try This :

private void chbFilter_CheckedChanged(object sender, EventArgs e)
{
    string filter = "";
    if (chbAktivan.Checked)
        filter += "ExpirationDate >= '{0}'," + DateTime.Today;
    if (chbNeaktivan.Checked)
    {
        filter += OR(filter);
        filter += "ExpirationDate < '{0}'," + DateTime.Today;
    }

    bs.Filter = string.Format(filter);
}

currently you are trying to concat string filter with a result of expression. ()

but filter expression for bindingSource should be provided in text format

filter += String.Format(" ExpirationDate >= '{0}' ", DateTime.Today);
...
filter += String.Format(" ExpirationDate < '{0}' ", DateTime.Today);

if i remember correct DateTime values should be written inside ' '

尝试使用此:

filter += "ExpirationDate >= " + DateTime.Today;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM