How do you filter a bound datagridview for a single month from a date/time field

Jay O'Brien 71 Reputation points
2023-05-15T16:39:05.0466667+00:00

I wish to add a filter to a large datagridview that will allow a user to view only rows where a date/time column is of a certain month. This will be one of many filters. Other filters are working well, but I receive undefined function when I try to use Filt = "Month(DateOn) = '" + tbMo1.Text + "'"; where DateOn is the name of the column.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,307 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 34,276 Reputation points Microsoft Vendor
    2023-05-16T06:10:31.0433333+00:00

    Hi @Jay O'Brien , Welcome to Microsoft Q&A.

    I used combobox instead of textbox to get the selected month.

    After clicking the button, perform a LinQ query on the Source to filter the data.

    Can this help you?

    private void button1_Click(object sender, EventArgs e)
    {
        if (int.TryParse(comboBox1.SelectedItem.ToString(), out int selectedMonth))
        {
            var filteredData = dataSource.Where(item => item.DateOn.Month == selectedMonth).ToList();
            dataGridView1.DataSource = filteredData.ToList();
        }
    }
    

    enter image description here

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Jay O'Brien 71 Reputation points
    2023-05-17T14:11:38.2433333+00:00

    I could not find a function to use in a filter; so, I added another field to my database built from Month(DateOn). While this was not the solution I was seeking, it was a workaround.

    0 comments No comments