Click here to Skip to main content
15,886,689 members
Articles / Desktop Programming / Windows Forms

DataGridView Filter Popup

Rate me:
Please Sign up or sign in to vote.
4.95/5 (249 votes)
20 Mar 2009CPOL8 min read 1M   65.4K   507  
A flexible library to add filtering capabilities to a DataGridView.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DgvFilterPopup;

namespace DgvFilterPopupDemo {
    public partial class Sample2 : DgvFilterPopupDemo.Sample0 {
        public Sample2() {
            InitializeComponent();
        }

        DgvDateColumnFilter OrderDate;

        private void Sample2_Load(object sender, EventArgs e) {
            InitGrid();

            DgvFilterManager fm = new DgvFilterManager();
            fm.DataGridView = dataGridView1; //after this line, column filters are created

            // Get the created column filter for OrderDate column
            OrderDate = ((DgvDateColumnFilter)fm["OrderDate"]);

            //Add some new operators
            OrderDate.ComboBoxOperator.Items.Insert(0, "This year");
            OrderDate.ComboBoxOperator.Items.Insert(1, "1 year ago");
            OrderDate.ComboBoxOperator.Items.Insert(2, "2 years ago");

            //Change the size to accomodate the length of the new operators
            OrderDate.ComboBoxOperator.Width += 30;
            OrderDate.DateTimePickerValue.Width -= 30;
            OrderDate.DateTimePickerValue.Location = new Point(OrderDate.DateTimePickerValue.Left + 30, OrderDate.DateTimePickerValue.Top);
            OrderDate.FilterExpressionBuilding += new CancelEventHandler(OrderDate_FilterExpressionBuilding);

        }

        void OrderDate_FilterExpressionBuilding(object sender, CancelEventArgs e) {
            int index = OrderDate.ComboBoxOperator.SelectedIndex;
            if (index < 3) { // the first 3 are the new operators
                int year = (DateTime.Today.Year - index);
                OrderDate.FilterExpression = "(OrderDate>='" + year.ToString() + "-1-1' AND OrderDate<='" + year.ToString() + "-12-31') ";
                OrderDate.FilterCaption = OrderDate.OriginalDataGridViewColumnHeaderText + "\n = year " + year.ToString();
                e.Cancel = true;
            }
        }


    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Italy Italy
I'm a graduate in Computer Science.
I work with Metatrader MQL4,MQL5 / C# / Asp.Net / Windows Forms / SQL Server / Access / VBA / HTML / CSS / Javascript / classic C/C++.


I also like writing songs and playing around with my band Diversamente Rossi.
This is the video of the song Un'altra estate from the album L'immobile disegno.



"Short code, good code"

Comments and Discussions