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

Posted on 2006-06-08
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
    LVL 34

    Expert Comment

    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.

    LVL 5

    Expert Comment

    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


    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


    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

    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

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    731 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now