Datagrid Leaving Bad Row Behind - How to Prevent this Behavior
Posted on 2005-03-28
The following code builds a dataset and dataview for a datagrid on a Windows form. It is executed everytime the user clicks on the btnSearch button after typing some text into tbText. FilteredMattersList takes a string argument and calls a stored procedure which returns rows that match the text in tbText. Because there are over 300k records in the table, I want the user to always specify a string to match on as opposed to returning all 300k records.. When the user leaves the field empty, I produce an error message. All of this works fine.
However, if the user specifies a string for which zero records are returned (i.e. "zzzzzzzzzz"), one row in the datagrid is left on the form just dangling in the grid at row position 1. It is not active and shouldn't really be there, but it is visible as well as annoying.
First dataview based upon tbText.Text = "ack" contains rows "bracket", "jacket" in that order.
Second dataview based upon tbText.Text = "zzzzzzzzzzzzz" contains no rows but the data grid shows a blank for row 0 and "bracket" in row 1 where "jacket" used to be even though it is a residual row and it doesn't really exist.
When I have a dataview that has no rows, how can I get the datagrid to show no residue from the previous dataview's contents?
Private Sub GetGridData()
If tbText.Text.Trim <> Nothing Then
cOriginalText = tbText.Text.Trim
dsMatters = oDataServices.FilteredMattersList(tbText.Text.Trim)
dvMatters = dsMatters.Tables("Matter").DefaultView
dvMatters.Sort = "Description"
dgMatters.DataSource = dvMatters
tbText.Text = cOriginalText
MsgBox("The Text box cannot be left empty." + vbCrLf + "Please enter text to filter on.", _
MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "Data Entry Required")