Click here to Skip to main content
15,889,931 members
Articles / Productivity Apps and Services / Microsoft Office / Microsoft Excel
Tip/Trick

Another DatagridView for Windows Forms

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
21 Dec 2018CPOL2 min read 8.6K   953   14  
Add Filter Sort and Theme to a datagridview

Image 1

Introduction

I wanted a datagridview where you could not only filter on the datasource but also sort and add a theme at the same time.

Background

In some of my projects, I needed something of a filter which looks a bit like the filter options of Excel. In the next version, I also want something like grouping and freezing of columns and rows.

Using the Code

What I wanted was a small filter picture in the columnheader of the datagridview. If you do a horizontal scroll and the columns are visible, then the pictures must be in the correct position. In the call of the paint event, there is the method I used for that purpose. If the column is of the type DataGridViewImageColumn, then there is no display a filter picture.

C#
private void Grid_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
	PaintFiltersOnColumnsHeaders(e);
}
C#
private void PaintFiltersOnColumnsHeaders(PaintEventArgs e)
{
	Graphics gr = e.Graphics;

	for (int colIdx = 0; colIdx < _grid.Columns.Count; colIdx++)
	{
		// check
		if (IsColumnDisplayed(colIdx))
		{
			Type colType = _grid.Columns[colIdx].GetType();

			// check
			if (colType != typeof(DataGridViewImageColumn))
			{
				if (_filters[colIdx] != null)
				{
					if (_filters[colIdx].IsFilter)
					{
						DrawFilter(colIdx, icoIsFilter, gr);
					}
					else
					{
						DrawFilter(colIdx, icoNoFilter, gr);
					}
				}
				else
				{
					DrawFilter(colIdx, icoNoFilter, gr);
				}
			}
		}
	}
}

Here the code to draw the filter picture on the columnheader:

C#
private void DrawFilter(int colIdx, Image ico, Graphics gr)
{
	Rectangle rect = _grid.GetCellDisplayRectangle(colIdx, -1, true);

	if (rect.Width > 20)
	{
		Rectangle rectPicture = new Rectangle
		{
			Width = 18,

			Height = rect.Height - 12,

			Location = new Point(rect.Left + (rect.Width - 20), rect.Top + 6)
		};

		gr.DrawImage(ico, rectPicture);
	}
}

If you click on the filter picture in the column header, then a form will be shown. With that form, you can select some filter options. You can also deselect a filter on a column. To deselect all the filters, right click in the grid and reset all filters.

Image 2

If you right click on the grid, then you have several options. You can sort and you can reset all the filters. For the sorting, you have to set the sortmode for all the columns. With the change of the datasource, you can set the correct sort mode. With the last method, you can set the sort direction.

C#
private void BaseDataGrid_DataSourceChanged(object sender, EventArgs e)
{
	ColumnFilter.ResetFilters();

	ColumnSorter.SetSortMode();
}
		
public void SetSortMode()
{
	for (int colTeller = 0; colTeller < _grid.Columns.Count; colTeller++)
	{
		_grid.Columns[colTeller].SortMode = DataGridViewColumnSortMode.Programmatic;
	}
}

public void SortZA(int colIdx)
{
	_sortDirection = 1;

	SetSorted(colIdx);          
}

For setting the theme, there is an interface for making your one. In this example, there is a theme include. I hope that the example will lead to new themes.

History

  • Version 19 December 2018 is the first.

License

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


Written By
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --