简体   繁体   中英

How do I filter a dataview by month from a datetime column?

I have a DataGridView which I'd like to filter by a month name from another combo. I have a column (datetime column, called "data")which holds the date values.

I guess, first I need to extract the month names from the date fields and compare it with the combobox selected item.

The data is received from mysql, and it'a a Windows Form.

So, in the datagrid i want to filter, the date field is called "data". The combo which contains the month names is filled:

for (int i = 0; i <= 12; i++)
        {
            comboBox3.Items.Add(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[i]);
        }

So, when i select a month from the combo, i want the records in the datagrid to be filtered, so that only the records from the month specified in the combo to be shown.

I don't know how to make the filter. The date field in the datagrid is ("yy-mm-dd"), and the combo contains the month names. I tried to format the date field in the datagrid to be "MMMM" - the same format as the combo, but it doesn't work, i guess because the values are still in yy-mm-dd format, only the appearance of the cells changed.

here's one solution...

dt.DefaultView.RowFilter = "CONVERT(SUBSTRING(CONVERT(data, 'System.String'), 4, 2), 'System.Int32')-1= " + comboBox3.SelectedIndex;

I aimed the index of the combo, and the filter gets the month number, not the name... I was using the Romanian culture, so for english it has to be adapted a little. thanks everyone...

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