Results 1 to 17 of 17

Thread: [RESOLVED] Strange DGV Behavior

  1. #1

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Resolved [RESOLVED] Strange DGV Behavior

    I have a DGV bound to a datatable. I then swap out a column and replace it with a dropdown combo using this code:

    Code:
    Dim dgvCol As New DataGridViewComboBoxColumn
                dgvCol.ValueType = GetType(String)
                dgvCol.DisplayMember = "Species"
                dgvCol.ValueMember = "Species"
                dgvCol.DataSource = mCharacteristics.Tables("Species")
                dgvCol.HeaderText = "Species"
                dgvCol.DataPropertyName = "Species"
                dgvCol.DefaultCellStyle.BackColor = Drawing.Color.White
    
                dgvData.Columns.Remove(dgvData.Columns("Species"))
                dgvData.Columns.AddRange(dgvCol)
                dgvCol.DisplayIndex = 6
    While it works, it does one odd thing. After selecting a value in one row, when I then click on the drop down in the next (or any other) row, the row I just set flashes briefly blue, and nothing else happens. On the second click, the drop down shows up.

    That brief blue flash suggests perhaps a change of focus is happening. It would clearly be better for the drop down to appear on the first click, not on the second, though.

    I don't have any experience with drop downs in DGV's. What is happening here?
    My usual boring signature: Nothing

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Strange DGV Behavior

    Are you adding the column after the DataGridView has rendered by any chance?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Strange DGV Behavior

    No, all of that is happening in the form constructor, so prior to it ever being drawn.
    My usual boring signature: Nothing

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: Strange DGV Behavior

    This is certainly odd behavior. What I'd personally do to track this down is to generate events related that are related to the column and do a debug/console WriteLine to see what events are being triggered on the first click. From there it should give you a more clear picture of what is at least happening before the flickering.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Strange DGV Behavior

    Perhaps the following might help.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Strange DGV Behavior

    Why are you using that code to replace an existing column in the first place? Why not just add the combo box column in the designer? I'm guessing that you are letting the grid create the columns when you bind data and then replacing one of the columns it creates. Don't do that. Create the column you want in the first place. If you set the DataPropertyName of the column then your data source will be correctly bound to it, even if AutoGenerateColumns is True. There's a demo in my CodeBank submission on the subject.

  7. #7

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Strange DGV Behavior

    I'm doing it because the internet told me to...of course.

    At first, I thought I had a reason to do it this way, because I thought that the set of columns would be variable, which meant that adding them at binding would make more sense. Having played around with the design, I realized that is not what I wanted to do. The set of columns is now fixed, with only the visibility of some of them being toggled. That makes adding the columns in the designer much more reasonable, so I'll make that change.
    My usual boring signature: Nothing

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Strange DGV Behavior

    Quote Originally Posted by Shaggy Hiker View Post
    I'm doing it because the internet told me to...of course.
    Stupid internet.

  9. #9

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Strange DGV Behavior

    No kidding. The internet had something that was WRONG!! I'm Soooooooo distraught.
    My usual boring signature: Nothing

  10. #10
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Strange DGV Behavior

    Quote Originally Posted by Shaggy Hiker View Post
    No kidding. The internet had something that was WRONG!!

    This is not funny ! Because of you, my world and live just collapsed wait a minute, I am sure this is fake. You should be in the world conspiracy or member of this group that think that the earth is not flat
    Last edited by Delaney; Oct 3rd, 2021 at 02:52 AM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  11. #11

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Strange DGV Behavior

    Having now made the change the JMC suggested, the performance does appear slightly quicker, but what I described in the initial post is still happening: When you select an item from the drop down the first time, it works just fine.

    After selecting something from the first drop down, I go to the next and click the drop down arrow. At that point, the first one flashes, and that's all. The second time I click the drop down arrow on the second drop down list, then the drop down works. So, the first time on a new drop down, the old drop down flashes and the new drop down list doesn't appear. I have to click the drop down arrow on the second list twice to get it to drop down.

    This is consistent behavior, but clearly not desirable.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Strange DGV Behavior

    One other thing that I just noticed is that this no longer works:
    Code:
    dgvData.Columns("Lot").Visible = SomeVariable
    I have a function that does essentially that line for a subset of the columns in the DGV. That was working, and seems like it still should be, but once I changed over to defining all the columns in the designer, it no longer appears to do anything at all. I can see that the .Visible property of the column is set correctly, but all the columns are visible in all cases.

    Along those lines, I also had a button that allowed the user to choose which columns to show/hide. That is still working, and it is setting the visibility of the columns correctly, as far as the property is concerned, but the columns are always visible, regardless of the setting.

    What's that about?
    My usual boring signature: Nothing

  13. #13
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Strange DGV Behavior

    That is strange. All I can think of is a corrupt designer file. Because the Visible property still works when you us the designer to setup the datagrigview. Maybe create a test form and try your code.

  14. #14

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Strange DGV Behavior

    Quote Originally Posted by jmcilhinney View Post
    Why are you using that code to replace an existing column in the first place? Why not just add the combo box column in the designer? I'm guessing that you are letting the grid create the columns when you bind data and then replacing one of the columns it creates. Don't do that. Create the column you want in the first place. If you set the DataPropertyName of the column then your data source will be correctly bound to it, even if AutoGenerateColumns is True. There's a demo in my CodeBank submission on the subject.
    I tried your code in the CodeBank and it's not working.

    Actually, it is working fine. In fact, it's an excellent example and very well written, but I felt it would be bad form not to start out with something totally useless, and "it's not working" is about as useless as things get.

    However, what I DID see from that example is that the behavior I am seeing is happening in my test project when I run your code. I expect that what is going on is that, when you select an item in the drop down, then go to the next drop down, the first thing that happens is that focus switches from the first cell to the second cell. So, the first click is changing the focus, and only once the second cell has focus does the drop down actually drop down.

    Now that I have more experience with where I am going with my design, this is more acceptable. It's not ideal, but in the form I am working on, the user will only very rarely use the drop down at all. Most of the time, they'll be setting a value and auto-filling all records. I might enhance that a bit by auto-filling just some selected rows.

    So, the next piece I have to get back to is figuring out why those fool columns are visible even when visible is set to false.
    My usual boring signature: Nothing

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Strange DGV Behavior

    Remember that cells in a DataGridView don't have controls in them by default. When you shift focus to a different cell, the data from the previous editing control has to be pushed to the previous cell, then the editing control removed, then the focus shifted to the new cell, then the same or new editing control embedded in the new cell. There's a lot going on there that the user just doesn't really see but will generate events in the grid.

  16. #16

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Strange DGV Behavior

    Ah, I did not know that. My use of a DGV has always been pretty simple. I do remember that we had to do that ourselves, rather manually, in VB6 for the MSFlexGrid, so it's interesting that they just formalized it. Still, it will do.
    My usual boring signature: Nothing

  17. #17

    Thread Starter
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: [RESOLVED] Strange DGV Behavior

    Got back to this today, and realized that the visibility issue was essentially due to all the columns being duplicated. I had made one or two minor errors in my rush the other day, including not noticing that every column was in the DGV twice. Once I got that cleared up, I also cleared up the visibility issue, and I don't really know how. I'm not sure which of the changes I made was the one that fixed the visibility issue, and I don't feel like breaking things again just to figure it out. One way or another, I was showing the columns wrong. Most likely, there was no visibility issue, it's just that, with every column being added a second time, I didn't notice that some columns were only there once, while others were there twice.

    Anyways, the whole thing is resolved, and it is based off the CodeBank example that JMC posted in post #6. I expanded that example to include four different drop downs, and based off a more complex table, but it's the same structure.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width