简体   繁体   中英

Datagridview Numeric Filter - .net

I'm attempting to filter a datagrid based on a column called "Op No#". When someone puts a value into the textbox I build up a string containing the filter and then apply that to the datagrid (I've omitted that part as it works with other, text, fields I've tried it on).

The problem I'm encountering is that when I put a number into the textbox and apply the filter it doesn't return any results and I can't see any reason for this. Any help would be greatly appreciated.

    If MainView.TextBox2.Text <> "" Then
        If MainView.FilterCount = 0 Then
            MainView.FilterStr = "'*[Op No#]*' like '%" & MainView.TextBox2.Text & "%'" : MainView.FilterCount = MainView.FilterCount + 1
        Else
            MainView.FilterStr = MainView.FilterStr & "and" & "'*[Op No#]*' like '%" & Convert.ToInt32(MainView.TextBox2.Text) & "%'" : MainView.FilterCount = MainView.FilterCount + 1
        End If
    End If

Your main filter string is wrong from what I can see... change this

"'*[Op No#]*' like '%" & MainView.TextBox2.Text & "%'" 

To this...for numbers only...

 "Column name = " & MainView.TextBox2.Text

Another Example...

 DataView.RowFilter = "Year = 2008"

Your filter syntax is wrong and you're doing a like on an integer...your wrapping it in ticks and percent symbols...dont do this.

Also theres a pound symbol after your column and thats fine, just make sure its wrapped in brackets like you have, i didnt show this.

String values are enclosed within single quotes ' '. If the string contains single quote ', the quote must be doubled. Number values are not enclosed within any characters. The values should be the same as is the result of int.ToString() or float.ToString() method for invariant or English culture.

Heres more information... http://www.csharp-examples.net/dataview-rowfilter/

Hope this helps...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM