|
| 1 | +--- |
| 2 | +title: How to Use GridView Filter Row in RadMultiColumnCombobox |
| 3 | +description: Learn how to use the grid's filter row in RadMultiColumnCombobox. |
| 4 | +type: how-to |
| 5 | +page_title: How to Use GridView Filter Row in RadMultiColumnCombobox |
| 6 | +slug: grid-filter-row-in-multicolumncobobox |
| 7 | +position: 5 |
| 8 | +tags: grid, filter, multicolumn |
| 9 | +ticketid: 1521435 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | + |
| 14 | +## Environment |
| 15 | +|Product Version|Product|Author| |
| 16 | +|----|----|----| |
| 17 | +|2021.2.511|RadMultiColumnCombobox|[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +This tutorial demonstrates a sample approach how to enable the filtering row in the popup grid and filter the records according to the user's input in the particular column. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## Solution |
| 26 | + |
| 27 | +Create an extension **Filtering** method for the **RadMultiColumnComboBox**. It enables the filtering row in the popup grid. |
| 28 | + |
| 29 | +````C# |
| 30 | +public partial class Form1 : Form |
| 31 | +{ |
| 32 | + public Form1() |
| 33 | + { |
| 34 | + InitializeComponent(); |
| 35 | + } |
| 36 | + |
| 37 | + private void Form1_Load(object sender, EventArgs e) |
| 38 | + { |
| 39 | + this.customersTableAdapter.Fill(this.nwindDataSet.Customers); |
| 40 | + |
| 41 | + this.radMultiColumnComboBox1.DataSource = this.customersBindingSource; |
| 42 | + this.radMultiColumnComboBox1.DisplayMember = "ContactName"; |
| 43 | + this.radMultiColumnComboBox1.ValueMember = "CustomerID"; |
| 44 | + this.radMultiColumnComboBox1.AutoSizeDropDownToBestFit = true; |
| 45 | + this.radMultiColumnComboBox1.AutoSizeDropDownColumnMode = BestFitColumnMode.AllCells; |
| 46 | + |
| 47 | + this.radMultiColumnComboBox1.Filtering(true); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +internal static class ExtensionsRadMultiColumnComboBox |
| 52 | +{ |
| 53 | + public static void Filtering(this RadMultiColumnComboBox combo, bool enable) |
| 54 | + { |
| 55 | + if (enable) |
| 56 | + { |
| 57 | + if (!combo.EditorControl.EnableFiltering) |
| 58 | + { |
| 59 | + combo.DropDownClosing += RadMultiColumnComboBoxDropDownClosing; |
| 60 | + |
| 61 | + var multiColumnComboBoxElement = combo.MultiColumnComboBoxElement; |
| 62 | + multiColumnComboBoxElement.EditorControl.EnableFiltering = true; |
| 63 | + multiColumnComboBoxElement.EditorControl.ShowFilteringRow = true; |
| 64 | + multiColumnComboBoxElement.EditorControl.FilterChanging += |
| 65 | + RadMultiColumnComboBoxEditorControlFilterChanging; |
| 66 | + } |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + if (combo.EditorControl.EnableFiltering) |
| 71 | + { |
| 72 | + combo.DropDownClosing -= RadMultiColumnComboBoxDropDownClosing; |
| 73 | + |
| 74 | + var multiColumnComboBoxElement = combo.MultiColumnComboBoxElement; |
| 75 | + multiColumnComboBoxElement.EditorControl.EnableFiltering = false; |
| 76 | + multiColumnComboBoxElement.EditorControl.ShowFilteringRow = false; |
| 77 | + multiColumnComboBoxElement.EditorControl.FilterChanging -= |
| 78 | + RadMultiColumnComboBoxEditorControlFilterChanging; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static void RadMultiColumnComboBoxEditorControlFilterChanging(object sender, |
| 84 | + GridViewCollectionChangingEventArgs e) |
| 85 | + { |
| 86 | + var multiColumnComboGridView = sender as MultiColumnComboGridView; |
| 87 | + if (multiColumnComboGridView == null) |
| 88 | + return; |
| 89 | + if (multiColumnComboGridView.Parent == null) |
| 90 | + return; |
| 91 | + var radPopupControlBase = multiColumnComboGridView.Parent as MultiColumnComboPopupForm; |
| 92 | + if (radPopupControlBase == null) |
| 93 | + return; |
| 94 | + if (!radPopupControlBase.IsDisplayed) |
| 95 | + return; |
| 96 | + if (e.OldItems == null) |
| 97 | + return; |
| 98 | + if (e.OldItems.Count == 0) |
| 99 | + return; |
| 100 | + var filterDescriptor = e.OldItems[0] as FilterDescriptor; |
| 101 | + if (filterDescriptor == null) |
| 102 | + return; |
| 103 | + var grid = multiColumnComboGridView; |
| 104 | + var currentRow = grid.CurrentRow; |
| 105 | + if (currentRow.RowElementType != typeof (GridFilterRowElement)) |
| 106 | + return; |
| 107 | + var value = currentRow.Cells[filterDescriptor.PropertyName].Value ?? string.Empty; |
| 108 | + if (value == null) |
| 109 | + return; |
| 110 | + string activeEditorString = null; |
| 111 | + if (grid.ActiveEditor != null) |
| 112 | + { |
| 113 | + var activeEditorValue = grid.ActiveEditor.Value ?? string.Empty; |
| 114 | + activeEditorString = activeEditorValue.ToString(); |
| 115 | + } |
| 116 | + if (e.NewValue == null && value.ToString() != string.Empty && |
| 117 | + !(activeEditorString != null && activeEditorString == string.Empty && |
| 118 | + grid.CurrentColumn.Name == filterDescriptor.PropertyName)) |
| 119 | + e.Cancel = true; |
| 120 | + } |
| 121 | + |
| 122 | + private static void RadMultiColumnComboBoxDropDownClosing(object sender, RadPopupClosingEventArgs args) |
| 123 | + { |
| 124 | + var radMultiColumnComboBox = sender as RadMultiColumnComboBox; |
| 125 | + if (radMultiColumnComboBox == null) |
| 126 | + return; |
| 127 | + var popupForm = radMultiColumnComboBox.MultiColumnComboBoxElement.PopupForm; |
| 128 | + var editorControl = radMultiColumnComboBox.MultiColumnComboBoxElement.EditorControl; |
| 129 | + MultiColumnComboBoxDropDownClosingHandler(popupForm, editorControl, args); |
| 130 | + } |
| 131 | + |
| 132 | + private static void MultiColumnComboBoxDropDownClosingHandler(RadPopupControlBase popupForm, |
| 133 | + RadGridView editorControl, RadPopupClosingEventArgs args) |
| 134 | + { |
| 135 | + var mousePosition = Control.MousePosition; |
| 136 | + var xIn = mousePosition.X >= popupForm.Location.X && |
| 137 | + mousePosition.X <= popupForm.Location.X + popupForm.Size.Width; |
| 138 | + var yIn = mousePosition.Y >= popupForm.Location.Y && |
| 139 | + mousePosition.Y <= popupForm.Location.Y + popupForm.Size.Height; |
| 140 | + if (editorControl.CurrentRow.Index == -1 && xIn && yIn) |
| 141 | + { |
| 142 | + args.Cancel = true; |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + if (editorControl.ActiveEditor != null) |
| 147 | + { |
| 148 | + editorControl.ActiveEditor.EndEdit(); |
| 149 | + } |
| 150 | + editorControl.FilterChanging -= RadMultiColumnComboBoxEditorControlFilterChanging; |
| 151 | + foreach (var column in editorControl.Columns) |
| 152 | + { |
| 153 | + column.FilterDescriptor = null; |
| 154 | + } |
| 155 | + editorControl.FilterChanging += RadMultiColumnComboBoxEditorControlFilterChanging; |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | + |
| 161 | +```` |
| 162 | +````VB.NET |
| 163 | + |
| 164 | +Public Class RadForm1 |
| 165 | + Private Sub RadForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load |
| 166 | + Me.CustomersTableAdapter.Fill(Me.NwindDataSet.Customers) |
| 167 | + |
| 168 | + Me.RadMultiColumnComboBox1.DataSource = Me.CustomersBindingSource |
| 169 | + Me.RadMultiColumnComboBox1.DisplayMember = "ContactName" |
| 170 | + Me.RadMultiColumnComboBox1.ValueMember = "CustomerID" |
| 171 | + Me.RadMultiColumnComboBox1.AutoSizeDropDownToBestFit = True |
| 172 | + Me.RadMultiColumnComboBox1.AutoSizeDropDownColumnMode = BestFitColumnMode.AllCells |
| 173 | + Me.RadMultiColumnComboBox1.Filtering(True) |
| 174 | + End Sub |
| 175 | +End Class |
| 176 | + |
| 177 | +Module ExtensionsRadMultiColumnComboBox |
| 178 | + <Extension()> |
| 179 | + Sub Filtering(ByVal combo As RadMultiColumnComboBox, ByVal enable As Boolean) |
| 180 | + If enable Then |
| 181 | + |
| 182 | + If Not combo.EditorControl.EnableFiltering Then |
| 183 | + AddHandler combo.DropDownClosing, AddressOf RadMultiColumnComboBoxDropDownClosing |
| 184 | + Dim multiColumnComboBoxElement = combo.MultiColumnComboBoxElement |
| 185 | + multiColumnComboBoxElement.EditorControl.EnableFiltering = True |
| 186 | + multiColumnComboBoxElement.EditorControl.ShowFilteringRow = True |
| 187 | + AddHandler multiColumnComboBoxElement.EditorControl.FilterChanging, AddressOf RadMultiColumnComboBoxEditorControlFilterChanging |
| 188 | + End If |
| 189 | + Else |
| 190 | + |
| 191 | + If combo.EditorControl.EnableFiltering Then |
| 192 | + RemoveHandler combo.DropDownClosing, AddressOf RadMultiColumnComboBoxDropDownClosing |
| 193 | + Dim multiColumnComboBoxElement = combo.MultiColumnComboBoxElement |
| 194 | + multiColumnComboBoxElement.EditorControl.EnableFiltering = False |
| 195 | + multiColumnComboBoxElement.EditorControl.ShowFilteringRow = False |
| 196 | + RemoveHandler multiColumnComboBoxElement.EditorControl.FilterChanging, AddressOf RadMultiColumnComboBoxEditorControlFilterChanging |
| 197 | + End If |
| 198 | + End If |
| 199 | + End Sub |
| 200 | + |
| 201 | + Private Sub RadMultiColumnComboBoxEditorControlFilterChanging(ByVal sender As Object, ByVal e As GridViewCollectionChangingEventArgs) |
| 202 | + Dim multiColumnComboGridView = TryCast(sender, MultiColumnComboGridView) |
| 203 | + If multiColumnComboGridView Is Nothing Then Return |
| 204 | + If multiColumnComboGridView.Parent Is Nothing Then Return |
| 205 | + Dim radPopupControlBase = TryCast(multiColumnComboGridView.Parent, MultiColumnComboPopupForm) |
| 206 | + If radPopupControlBase Is Nothing Then Return |
| 207 | + If Not radPopupControlBase.IsDisplayed Then Return |
| 208 | + If e.OldItems Is Nothing Then Return |
| 209 | + If e.OldItems.Count = 0 Then Return |
| 210 | + Dim filterDescriptor = TryCast(e.OldItems(0), FilterDescriptor) |
| 211 | + If filterDescriptor Is Nothing Then Return |
| 212 | + Dim grid = multiColumnComboGridView |
| 213 | + Dim currentRow = grid.CurrentRow |
| 214 | + If currentRow.RowElementType <> GetType(GridFilterRowElement) Then Return |
| 215 | + Dim value = If(currentRow.Cells(filterDescriptor.PropertyName).Value, String.Empty) |
| 216 | + If value Is Nothing Then Return |
| 217 | + Dim activeEditorString As String = Nothing |
| 218 | + |
| 219 | + If grid.ActiveEditor IsNot Nothing Then |
| 220 | + Dim activeEditorValue = If(grid.ActiveEditor.Value, String.Empty) |
| 221 | + activeEditorString = activeEditorValue.ToString() |
| 222 | + End If |
| 223 | + |
| 224 | + If e.NewValue Is Nothing AndAlso value.ToString() <> String.Empty AndAlso Not (activeEditorString IsNot Nothing AndAlso |
| 225 | + activeEditorString = String.Empty AndAlso grid.CurrentColumn.Name = filterDescriptor.PropertyName) Then e.Cancel = True |
| 226 | + End Sub |
| 227 | + |
| 228 | + Private Sub RadMultiColumnComboBoxDropDownClosing(ByVal sender As Object, ByVal args As RadPopupClosingEventArgs) |
| 229 | + Dim radMultiColumnComboBox = TryCast(sender, RadMultiColumnComboBox) |
| 230 | + If radMultiColumnComboBox Is Nothing Then Return |
| 231 | + Dim popupForm = radMultiColumnComboBox.MultiColumnComboBoxElement.PopupForm |
| 232 | + Dim editorControl = radMultiColumnComboBox.MultiColumnComboBoxElement.EditorControl |
| 233 | + MultiColumnComboBoxDropDownClosingHandler(popupForm, editorControl, args) |
| 234 | + End Sub |
| 235 | + |
| 236 | + Private Sub MultiColumnComboBoxDropDownClosingHandler(ByVal popupForm As RadPopupControlBase, ByVal editorControl As RadGridView, ByVal args As RadPopupClosingEventArgs) |
| 237 | + Dim mousePosition = Control.MousePosition |
| 238 | + Dim xIn = mousePosition.X >= popupForm.Location.X AndAlso mousePosition.X <= popupForm.Location.X + popupForm.Size.Width |
| 239 | + Dim yIn = mousePosition.Y >= popupForm.Location.Y AndAlso mousePosition.Y <= popupForm.Location.Y + popupForm.Size.Height |
| 240 | + |
| 241 | + If editorControl.CurrentRow.Index = -1 AndAlso xIn AndAlso yIn Then |
| 242 | + args.Cancel = True |
| 243 | + Else |
| 244 | + |
| 245 | + If editorControl.ActiveEditor IsNot Nothing Then |
| 246 | + editorControl.ActiveEditor.EndEdit() |
| 247 | + End If |
| 248 | + |
| 249 | + RemoveHandler editorControl.FilterChanging, AddressOf RadMultiColumnComboBoxEditorControlFilterChanging |
| 250 | + |
| 251 | + For Each column In editorControl.Columns |
| 252 | + column.FilterDescriptor = Nothing |
| 253 | + Next |
| 254 | + |
| 255 | + AddHandler editorControl.FilterChanging, AddressOf RadMultiColumnComboBoxEditorControlFilterChanging |
| 256 | + End If |
| 257 | + End Sub |
| 258 | +End Module |
| 259 | + |
| 260 | + |
| 261 | +```` |
| 262 | + |
| 263 | +# See Also |
| 264 | + |
| 265 | +* [Filtering in RadMultiColumnComboBox]({%slug winforms/multicolumncombobox/filtering%}) |
| 266 | + |
0 commit comments