Filtering a datagridview
assuming you have a datagridview that has data in it.
you want to avoid recalling sql from the database & just want to filter the datagrid.
you can do the following, What you need (1 datagridview (data), 1 button (search), 1 textbox (string to search), 1 variable to precise the criteria your searching for)
Dim findcrit as string
Dim dt As DataTable = Ds_Pos.Employees
Dim dv As New DataView(dt)
Dim _RowFilter As String = ""
Dim _FieldType = dt.Columns(findcrit).DataType.ToString
Select Case _FieldType
Case "System.Int32"
_RowFilter = "convert(" & findcrit & ", 'System.String') like '" & Me.TextBox1.Text & "'"
Case "System.Int64"
_RowFilter = "convert(" & findcrit & ", 'System.String') like '" & Me.TextBox1.Text & "'"
Case "System.Double"
_RowFilter = "convert(" & findcrit & ", 'System.String') like '" & Me.TextBox1.Text & "'"
Case "System.String"
_RowFilter = findcrit & " like '" & TextBox1.Text & "*'"
End Select
dv.RowFilter = _RowFilter
EmployeesDataGridView.DataSource = dv
The cases are to cast the type of variable from any type to string because you can only use the method on string data types.
the idea is to move the content to a data table then filter then return it back to the datatable.
Leave a Reply
You must be logged in to post a comment.