Filter data in DataGridView
-
Is there a way to adjust the contents to a DataGridView to match what a user has typed in a textbox. As the user types in or removes a character from the text box, the DataGridView contents will adjust accordingly from the intial data populating the control from a Datatable. I would prefer to avoid loops, if at all possible. I would be willing to use a ListView control. I also tried to look for a method in a DataSet or DataTable and populate a second DataTable to bind to the grid, but could find a way. Any ideas? Thank You
-
Is there a way to adjust the contents to a DataGridView to match what a user has typed in a textbox. As the user types in or removes a character from the text box, the DataGridView contents will adjust accordingly from the intial data populating the control from a Datatable. I would prefer to avoid loops, if at all possible. I would be willing to use a ListView control. I also tried to look for a method in a DataSet or DataTable and populate a second DataTable to bind to the grid, but could find a way. Any ideas? Thank You
Not sure if I understand your question entirely, but you could try this: Dim dt as New Datatable ' 'Code to fill your table ' dim dv as new dataview(dt) Datagrid1.datasource = dv 'Then in the Textbox TextChanged Event, you can modify the filter value Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged ' "Name" is a column name in your datatable dv.RowFilter = "Name LIKE ' " & Me.Textbox1.Text & " * ' " 'This will filter for anything that begins with whatever is typed in the textbox End Sub
-
Not sure if I understand your question entirely, but you could try this: Dim dt as New Datatable ' 'Code to fill your table ' dim dv as new dataview(dt) Datagrid1.datasource = dv 'Then in the Textbox TextChanged Event, you can modify the filter value Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged ' "Name" is a column name in your datatable dv.RowFilter = "Name LIKE ' " & Me.Textbox1.Text & " * ' " 'This will filter for anything that begins with whatever is typed in the textbox End Sub
Thanks. Your code did exactly what I needed to do. I tweaked it somewhat. Here it is. Private Sub txtSysName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSysName.TextChanged 'if nothing in textbox, display all rows If Me.txtSysName.Text.Trim.Length = 0 Then DataGridView1.DataSource = ds.Tables(0) Msg.Text = "System count: " & DataGridView1.Rows.Count Exit Sub End If 'create dataview and populate with dataset populated in form load Dim dv As New DataView(ds.Tables(0)) With dv .Table = ds.Tables(0) '.AllowDelete = True '.AllowEdit = True '.AllowNew = True .RowFilter = "vchrName LIKE '" & Me.txtSysName.Text & "*'" .RowStateFilter = DataViewRowState.CurrentRows .Sort = "vchrName ASC" End With 'reset grid datasource to contents of new dataview DataGridView1.DataSource = dv 'display row count Msg.Text = "System count: " & DataGridView1.Rows.Count End Sub