Preventing a dataGrid cell remaining visible if clicked on, after a fresh list of data is displayed

I have a listBox than contains values from a dataTable.

I also have a DataGrid that is populated based on the listBox.SelectedValue (value used as the dataAdaptor.SelectCommand.Parameter that populates the datatable the dataGrid is bound to). So every time a selected value in the listBox is changed, the contents of the dataGrid change accordingly.


Say the dataGrid lists 5 rows, the user puts the curser in (any) cell on the last row, then selects another value in the listBox. This then generates say only 3 rows. The problem is that the cell which was previously clicked on remains visible. Just that cell!

This problem applies to any cell clicked on, in any row that is greater than the new list. If it is less than, then the new (greater number of) rows overwrites the problem, then all is good. It is if the dataGrid is not clearing itself properly. Any suggestions on how to resolve this issue?

This method I have used to re-populate the dataGrid:

Private Sub listBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles listBox.SelectedIndexChanged
        MyDataAdaptor.SelectCommand.Parameters("@Column").Value = CType(Me.listBox.SelectedValue, DataRowView)( listBox.DisplayMember.ToString).ToString()

    End Sub
Who is Participating?

That's interesting.  I've now been able to reproduce the problem.  It's not just restricted to cases in which the number of rows after a change is less than it was before - although that is when it is most obvious.  If you click on a cell in the first row and then refresh, the value from the previous record will remain in the new record until you actually click somewhere on the datagrid.  I'd not come across this before, but I see you are not alone with it

The answer appears, however, to be fairly simple.  If you just add the line


as the last line of your lstBom_SelectedIndexChanged sub, the symptom disappears.  Or at least it did in my tests.  I had changed your code slightly for those.  I used the Northwind database for my data, and refreshed the dgdComp by binding it to a dataview and applying a rowfilter to that, rather than keep requerying the database.  But that change didn't get rid of the problem.  I didn't expect it to.  It just seemed a more efficient way of handling the data.  So I would expect the solution to work just as well with any other approach so far as handling the data is concerned.

I think we need more details here.  I haven't come across this one before and I've not been able to replicate the problem.  I've tried it making the listbox selection both with mouse and tabbing to it.

When you say that "that the cell which was previously clicked on remains visible" how does this work.  If there were 10 rows, and a cell was highlighted in the 10th, and there are now 5, is the "extra" cell that remains highlighted still on the 10th row - that is, there are a number of empty rows before it?  Or is it moved up so that it is just one "extra" row to contain this "extra" cell?

Is your datatable bound direct to the datatable, or is there a dataview standing between them?

What, if any, code do you have relating to your datagrid's events?

Do you have a CurrencyManager explicitly declared and, if so, might that be forcing (or trying to force) a .Position to a record which no longer exists?

I'm thrashing about a bit here, but as I can't reproduce the problem I'm just wondering where to start looking.  I assume you will have tried MyDataGrid.Refresh.

I've been out of VB.NET for a little bit, but I'll try to offer something...

You can loop through the Datagrid manually and set anything greater than your datagrid rows to be invisible...

dim i as integer
for i = (datagrid1.items.count - 1) to datagrid1.length    ''??not sure on how you get the max length of your datagrid''
  if datagrid1.items(i).Visible = False     ' might have to loop through each cell and set visible to false, not sure here.
next i

Might throw an error and I don't know if you want something that un-elegant...just throwing it out there to see if it helps
IvanHowarthAuthor Commented:

The following link is the test project (built using Visual Studio 2003) and three screen shots of the problem which hopefully will answer all those questions. I assure you that there is no malicious code contained. (You will also find some code for drawing lines, which has yet to work as I'm learning this from scratch. This test project is designed to eventually print the lists from the database, but I think a lot more head in the books is still required yet)

The link is valid for the next 7 days.

IvanHowarthAuthor Commented:
Thanks Roger - Perfect!

I hear what you say about using a dataview as a more efficient way of handling the data. This is only a test project, nevertheless, I will include it on future ones to develop good practices and of course, experience.

Thanks again!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.