Skip to content

Instantly share code, notes, and snippets.

@Zujaj
Last active October 12, 2020 10:32
Show Gist options
  • Save Zujaj/f24832893f3866c70ae545b44ae355a4 to your computer and use it in GitHub Desktop.
Save Zujaj/f24832893f3866c70ae545b44ae355a4 to your computer and use it in GitHub Desktop.
Demonstrates the code for DataGridViewAutoFilter TestApp
using DataGridViewAutoFilter;
using MoreLinq;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace TestApp
{
public partial class Form1 : Form
{
// Object of Person model
private Person personObject = new Person();
// List to hold person details
private List<Person> personList = new List<Person>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Invoke the method PopulateList using the person object
// Pass the personList as the parameter.
personObject.PopulateList(personList);
// Use morelinq ToDataTable extension for converting personList to DataTable.
// Assign the converted Datatable to BindingSource's DataSource.
personBindingSource.DataSource = personList.ToDataTable();
//Finally assign personBindingSource to DataGridView's DataSource.
dataGridView1.DataSource = personBindingSource;
//Allow data filtering
EnableGridFilter(true);
}
private void EnableGridFilter(bool value)
{
RankColumn.FilteringEnabled = value;
OccupationColumn.FilteringEnabled = value;
JobColumn.FilteringEnabled = value;
SalaryColumn.FilteringEnabled = value;
RateColumn.FilteringEnabled = value;
DateColumn.FilteringEnabled = value;
}
private void ShowAllLabel_Click(object sender, EventArgs e)
{
DataGridViewAutoFilterColumnHeaderCell.RemoveFilter(dataGridView1);
}
private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
string filterStatus = DataGridViewAutoFilterColumnHeaderCell.GetFilterStatus(dataGridView1);
if (string.IsNullOrEmpty(filterStatus))
{
ShowAllLabel.Visible = false;
FilterStatusLabel.Visible = false;
}
else
{
ShowAllLabel.Visible = true;
FilterStatusLabel.Visible = true;
FilterStatusLabel.Text = filterStatus;
}
}
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt
&& (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up)
&& dataGridView1.CurrentCell != null
&& dataGridView1.CurrentCell.OwningColumn.HeaderCell is DataGridViewAutoFilterColumnHeaderCell filterCell)
{
filterCell.ShowDropDownList();
e.Handled = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment