Skip to content

Commit 57e3365

Browse files
ES-976509 - Resolve Issues in Public Syncfusion Code Examples for DataGrid XAML Controls - Phase 2
1 parent ecc0c32 commit 57e3365

File tree

1 file changed

+179
-1
lines changed

1 file changed

+179
-1
lines changed

README.md

Lines changed: 179 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,179 @@
1-
**[View document in Syncfusion WinForms Knowledge base](https://www.syncfusion.com/kb/4409/how-to-add-filter-in-winforms-gridcontrol)**
1+
# How to add filter in WinForms GridControl?
2+
3+
This sample illustrates how to add filter in WinForms GridControl.
4+
5+
There is no built-in support for having a filter bar in a [WinForms GridControl](https://www.syncfusion.com/winforms-ui-controls/grid-control), because the GridFilterBar uses DataView’s RowFilter property for filtering.
6+
7+
If you are using the GridControl in virtual mode with a data table then the filter bar function can be implemented. In the given sample, the GridControlFilterBar is created to enable filter bar in grid control.
8+
9+
### Creating the GridControlFilterBar:
10+
11+
``` csharp
12+
public class GridControlFilterBar
13+
{
14+
private GridControl _grid;
15+
private DataView filterView;
16+
private DataView originalView;
17+
public int RowCount;
18+
public int FilterRowIndex = 1;
19+
20+
//Wire the Grid with the filterbar
21+
public void WireGrid(GridControl grid, DataTable dt)
22+
{
23+
this._grid = grid;
24+
this.originalView = dt.DefaultView;
25+
this.filterView = new DataView(dt);
26+
RowCount = dt.Rows.Count + 1;
27+
_grid.Model.Rows.FrozenCount += 1;
28+
if(this._grid != null)
29+
{
30+
this._grid.CurrentCellAcceptedChanges += new CancelEventHandler(_grid_CurrentCellAcceptedChanges);
31+
this._grid.CurrentCellCloseDropDown += new Syncfusion.Windows.Forms.PopupClosedEventHandler(_grid_CurrentCellCloseDropDown);
32+
this._grid.QueryCellInfo += new GridQueryCellInfoEventHandler(_grid_QueryCellInfo);
33+
_grid.Refresh();
34+
}
35+
}
36+
37+
bool inUnwire = false;
38+
39+
//Unwire the Grid from the filter
40+
public void UnwireGrid()
41+
{
42+
this._grid.CurrentCellAcceptedChanges -= new CancelEventHandler(_grid_CurrentCellAcceptedChanges);
43+
this._grid.CurrentCellCloseDropDown -= new Syncfusion.Windows.Forms.PopupClosedEventHandler(_grid_CurrentCellCloseDropDown);
44+
this._grid.QueryCellInfo -= new GridQueryCellInfoEventHandler(_grid_QueryCellInfo);
45+
originalView.RowFilter = "";
46+
this.RowCount = originalView.Table.Rows.Count;
47+
_grid.Model.Rows.FrozenCount -= 1;
48+
inUnwire = true;
49+
for(int i = 1;i<_grid.ColCount;i++)
50+
_grid[FilterRowIndex,i].Text ="";
51+
_grid.Refresh();
52+
this._grid = null;
53+
inUnwire = false;
54+
}
55+
56+
//Return the Grid is wired or not.
57+
public bool IsWired
58+
{
59+
get{return (this._grid!= null)&&!inUnwire;}
60+
}
61+
62+
//Data table for creating a unique entries
63+
protected virtual DataTable CreateUniqueEntries(string colName)
64+
{
65+
DataRow row1;
66+
DataTable table1 = new DataTable(colName);
67+
table1.Columns.Add(new DataColumn(colName));
68+
row1 = table1.NewRow();
69+
row1[0] = "[None]";
70+
table1.Rows.Add(row1);
71+
string text1 = "";
72+
ArrayList tempArray = new ArrayList();
73+
filterView.Sort = colName +" ASC";
74+
for (int num1 = 0; num1 < filterView.Count; num1++)
75+
{
76+
text1 = filterView[num1].Row[colName].ToString();
77+
if(tempArray.Count==0 || !tempArray.Contains(text1))
78+
{
79+
row1 = table1.NewRow();
80+
row1[0] = text1;
81+
tempArray.Add(text1);
82+
table1.Rows.Add(row1);
83+
}
84+
}
85+
return table1;
86+
}
87+
88+
//Filter collection
89+
ArrayList filters = new ArrayList();
90+
91+
struct filter
92+
{
93+
public string colname,filterString;
94+
public filter(string colname, string filterString)
95+
{
96+
this.colname = colname;
97+
this.filterString = filterString;
98+
}
99+
}
100+
101+
//Setting the filter condition
102+
public void SetFilters()
103+
{
104+
string FilterString = "";
105+
foreach(filter fil in filters)
106+
{
107+
if(filters.IndexOf(fil)>0)
108+
FilterString += " AND ";
109+
110+
FilterString += "["+fil.colname+"] = "+fil.filterString;
111+
}
112+
originalView.RowFilter = FilterString;
113+
RowCount = originalView.Count+1;
114+
_grid.Refresh();
115+
}
116+
}
117+
```
118+
119+
### Event Handlers for the GridControlFilterBar:
120+
121+
``` csharp
122+
private void _grid_CurrentCellAcceptedChanges(object sender, CancelEventArgs e)
123+
{
124+
GridCurrentCell cc = this._grid.CurrentCell;
125+
if(cc.ColIndex>0 && cc.RowIndex ==1)
126+
{
127+
foreach(filter fil in filters)
128+
{
129+
if(fil.colname == originalView.Table.Columns[cc.ColIndex - 1].ColumnName)
130+
{
131+
filters.Remove(fil);
132+
break;
133+
}
134+
}
135+
if(cc.Renderer.StyleInfo.Text != "[None]")
136+
filters.Add(new filter(originalView.Table.Columns[cc.ColIndex - 1].ColumnName,"'" + cc.Renderer.StyleInfo.Text + "'"));
137+
SetFilters();
138+
}
139+
}
140+
private void _grid_CurrentCellCloseDropDown(object sender, Syncfusion.Windows.Forms.PopupClosedEventArgs e)
141+
{
142+
GridCurrentCell cc = this._grid.CurrentCell;
143+
if(cc.ColIndex>0 && cc.RowIndex ==1)
144+
cc.ConfirmChanges();
145+
}
146+
private void _grid_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
147+
{
148+
if(e.ColIndex>0 && e.RowIndex == FilterRowIndex)
149+
{
150+
e.Style.CellType = GridCellTypeName.ComboBox;
151+
e.Style.ExclusiveChoiceList = true;
152+
e.Style.DataSource = CreateUniqueEntries(originalView.Table.Columns[e.ColIndex - 1].ColumnName);
153+
e.Style.ValueMember = originalView.Table.Columns[e.ColIndex - 1].ColumnName;
154+
}
155+
}
156+
```
157+
158+
### Wiring the Grid with the GridControlFilterBar:
159+
160+
``` csharp
161+
GridControlFilterBar filterBar;
162+
private void Form1_Load(object sender, System.EventArgs e)
163+
{
164+
//Creating the object for GridControlFilterBar
165+
filterBar = new GridControlFilterBar();
166+
filterBar.WireGrid(this.gridControl1,this.dt);
167+
168+
//Hook the event to wire/unwire the grid from filter
169+
this.btnWire.Click += btnWire_Click;
170+
}
171+
void btnWire_Click(object sender, EventArgs e)
172+
{
173+
//Wire/unwire the Grid from the filter bar
174+
if (this.filterBar.IsWired)
175+
this.filterBar.UnwireGrid();
176+
else
177+
this.filterBar.WireGrid(this.gridControl1, this.dt);
178+
}
179+
```

0 commit comments

Comments
 (0)