Mark
Mark

Reputation: 1400

DataGridView Column Header Cell Drop-Down Filter List, make case insensitive

As per this item I have added code to a datagridview to automatically add column filtering list to certain column headers.

 Private Sub DataGridView1_BindingContextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.BindingContextChanged
    For Each col As DataGridViewColumn In DataGridView1.Columns
        If col.Name = "rating" Or col.Name = "check" Or col.Name = "artist" Or col.Name = "genre" Or col.Name = "album" Or col.Name = "year" Or col.Name = "bpm" Or col.Name = "cover" Then
            col.HeaderCell = New DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell)
        End If
    Next
End Sub

Private Sub DataGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
    FilterText = DataGridViewAutoFilterColumnHeaderCell.GetFilterStatus(DataGridView1)
    If String.IsNullOrEmpty(FilterText) Then FilterText = ""
End Sub

However if my database bound column data contains the same string but with different cases the filter list contains multiple entries. For example my data for the datagridview artist column might contain "Abba" and "ABBA" and the datagridview auto generated column header list contains both case versions. How can I make the list case insensitive without modifying the underlying database data case so I only see "Abba" once in the list. (note I do not create the list as it is generated automatically from the bound datagridview database source data, see the link above)

enter image description here

Upvotes: 1

Views: 2209

Answers (1)

Mark
Mark

Reputation: 1400

Although the code provided by Jimi did not resolve the issue it did give me a hint as to where to look. I needed to expose PopulateFilters() to get to the code where I could manipulate the data being created for the Datagridview column header filter lists. To do this I downloaded the example given in the link above. From this I added DataGridViewAutoFilter.dll to my project and added the reference "Imports DataGridViewAutoFilter". This allowed me to open the public class DataGridViewAutoFilterColumnHeaderCell which contained the PopulateFilters() sub. To prevent case duplicates being created in the list I simply created a List Of String

Dim valueitems As New List(Of String)
For Each value As Object In list
     If Not valueitems.Contains(value.ToString.ToLower) Then
            valueitems.Add(value.ToString.ToLower)

            'original PopulateFilters code...
     End If
 Next value

Obviously there are other methods I could have used to get rid of duplicates however this worked for me.

Upvotes: 1

Related Questions