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

Jay O'Brien 71 Reputation points

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.

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,528 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 41,736 Reputation points Microsoft Vendor

    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,


    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

    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