简体   繁体   中英

How to use DATEADD(SQL) in DataView Rowfilter? (C#)

I`m making program to display information using DataGridView and ComboBox Selection.

In SQL, query is

SELECT ListID, ListTitle, ListLastModifyDate  WHERE ListLastModifyDate <= DATEADD(MM, -1, GETDATE())

I already built a code.

  • Write a text in Text Box, then Click Start Button.

  • Matched data showed in DataGridView

And now I want to add this..

  • If I choose item in Combobox, selected item`s result showed in DatagridView
  • I want to show data without change datasource

So I tried to use DataView`s RowFilter.. but some error found.

Code is below

private void mtcbSiteColSearchCondition_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataView dvSiteCol = new DataView(dtSiteCol);

        if (mtcbSiteColSearchCondition.SelectedItem.ToString() == "all")
        {
            mgrdSiteCollections.DataSource = dtSiteCol;
        }
        else
        {
            DateTime lastModifiedDate = DateTime.Now.AddMonths(-1);
            dvSiteCol.RowFilter = string.Format("SiteColLastModifyDate <= {0}",lastModifiedDate.ToString("yyyy/MM/dd"), mtcbSiteColSearchCondition.SelectedItem.ToString());
            mgrdSiteCollections.DataSource = dvSiteCol;
        }
    }

I`m not sure what is right...

Please somebody help me how can I change that query...

Thanks

The rules to use when applying a RowFilter are listed in the Expression property of the DataColumn object.

In particular when filtering on a DateTime value you should enclose your Date value inside the # symbol and express your date in the Invariant culture format

So you should write

  dvSiteCol.RowFilter = string.Format("SiteColLastModifyDate <= #{0}#",
                                      lastModifiedDate.ToString("MM/dd/yyyy")); 

It is no clear what do you want to do with this part of your code. It is not needed in the format expression, so you should remove this: ,mtcbSiteColSearchCondition.SelectedItem.ToString());

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