Link to home
Start Free TrialLog in
Avatar of Edward Stevens
Edward StevensFlag for United States of America

asked on

Datagrid Leaving Bad Row Behind - How to Prevent this Behavior

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.

Example:

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
                  dgMatters.Focus()
            Else
                  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")
                  tbText.Focus()
            End If
      End Sub
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Is the DataGrid in read-only mode?

Bob
Avatar of Edward Stevens

ASKER

Yes - the datagrid is in read-only mode so that users will not try to update the data from there.  I am using the grid as a selection tool.  The user will eventually be presented with a full editing form to make their changes to the data.

Ed
The DataView has an AllowNew property:

 dsMatters.Tables("Matter").DefaultView.AllowNew = False.

Bob
I applied your suggestion.  It did not help.  However, I paid a bit more attention to the issue this time and have some info to add.

Where I had mentioned that the original datagrid row 0 was remaining on the grid as row 1...   ...that's not true.  The residual row is remaining at the row position from which it was originally painted.  It is the row that was selected when the datasource was changed to a dataview without any rows.

Example:  If row 5 was selected when the empty dataview is assigned as the data source, then row 5 is the residual row and it is still painted in row 5's position.

Ed
What happens if you dgMatters.Refresh?

Bob
Bob:

Sorry it took me a few days to get back to you.  I did the dgMatters.Refresh but no dice.  The problem persists.  This appears to be a real stumper.

Ed
Well, Ed, since I am not the DataGrid expert (I don't really use the stupid thing), I am not sure if I can find a way to help with this one.  I was hoping to go for the easy one, but it doesn't look that I was going to get that lucky :(

Bob
Thanks Bob.  I appreciate all of your attempts.

I'll leave the question out here a little longer to see if anyone else wants to take a crack at it.

Ed
Ok, but I really doubt that others will see it now.  They generally don't want to waste their time.  I would ask for a refund, and reask this question, and then I won't respond.

Bob
Bob:

I have an annual subscription so I don't think a refund is a concern to me.  However, I do have a question based upon your statement.  I have never gone into Expert Mode so I am unfamiliar with how that works.  What did you mean others won't see it?

Ed
These are my observation about how E-E works:

There are many factors with E-E for experts IMHO:

(1) Interest
(2) Timing of question
(3) Level of expertise
(4) Disrespect from the askers
(5) Language barriers
(6) Misunderstandings

The general rule is:  If you don't get a question answered to your liking, then there are a couple of options:

(1) Post a 20-point question pointing to this one, so that it gets moved back to the top of the list (Most experts don't take the time to scroll, since it is generally assumed that someone is already on it, and it isn't worth their time).

(2) Post a free question in Community Support as ask for a refund:
https://www.experts-exchange.com/Community_Support/

The contributing experts will be able to respond as to whether they think that it should be deleted or not, so always remember that respect above anything is paramount.


Bob

Thanks for all of your help Bob.

I have re-posted the question.
Would you like this question deleted?

Bob
ASKER CERTIFIED SOLUTION
Avatar of amyhxu
amyhxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of amyhxu
amyhxu

I wrote my own code to simulate the same behavior above, seems it's working. But I am not sure if it will always work under any condition:

   dgMatters.ReadOnly = True

   Private Sub dgMatters_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgMatters.MouseUp
        dgMatters.Select(dgMatters.CurrentRowIndex)
    End Sub
amyhxu:

I have been and still am out of the office.  Sorry for not getting back to you sooner.  I will try this suggestion on Monday when I return to the office.  Thanks
amyhxu,

I will be attempting your suggestion in the next hour or so.  I will post my results at that time.  Thanks
amyhxu,

Is your example making an assumption that the data view has at least one row?  From the code that I see, it looks like there needs to be a row to select.  My issue is that the data view is generated by a SQL stored procedure, which during this condition, is returning no rows.  Keep in mind that the data view has no physical rows and there is no filter on the data view.  In circumstances where my data view has at least one row and the "filter" does not include any of those rows, the data grid works fine as is and shows a blank area..

Ed
amyhxu,

I tried a few variations of your code and got it to work.  Thanks much for the concepts!  I can finally move on to more important things.  Yippy!   :)

Ed