Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2006-06-08
Medium Priority
Last Modified: 2010-04-23
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
Question by:IvanHowarth
  • 2
  • 2
LVL 34

Expert Comment

ID: 16862445
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.


Expert Comment

ID: 16873935
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

Author Comment

ID: 16880148

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.


LVL 34

Accepted Solution

Sancler earned 2000 total points
ID: 16882241

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.


Author Comment

ID: 16893177
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!


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question