Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Fixing flicker in a DataGridView?

Fixing flicker in a DataGridView?

Scheduled Pinned Locked Moved C#
helpquestion
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Andrew Stampor

    I am using a DataGridView to display a List of objects that change frequently (at a rate of up to 4 times per second.) I am using a BindingSource as the DataSource for the DataGridView and on every datachange event I set the BindingSource's DataSource to the List of objects. This results in a bunch of flicker. I attempted to fix this using SuspendLayout and ResumeLayout, but haven't seen a difference. Any ideas?

    O Offline
    O Offline
    Office Lineman
    wrote on last edited by
    #2

    Andrew Stampor wrote:

    I am using a BindingSource as the DataSource for the DataGridView and on every datachange event I set the BindingSource's DataSource to the List of objects.

    What event do you mean by "datachange event"? Unless you're getting an entirely different list object than you were previously bound to, why are you setting the BindingSource's DataSource multiple times? -- I've killed again, haven't I?

    A 1 Reply Last reply
    0
    • O Office Lineman

      Andrew Stampor wrote:

      I am using a BindingSource as the DataSource for the DataGridView and on every datachange event I set the BindingSource's DataSource to the List of objects.

      What event do you mean by "datachange event"? Unless you're getting an entirely different list object than you were previously bound to, why are you setting the BindingSource's DataSource multiple times? -- I've killed again, haven't I?

      A Offline
      A Offline
      Andrew Stampor
      wrote on last edited by
      #3

      Thanks for your reply. I've changed the way I am trying to do this, but am not getting an update like I would like. What I am now doing is maintaining a List<CSomeObject>. That List is assigned as the datasource for the DataGridView. What I am noticing, though, is that when I update a value in one of the CSomeObjects that is part of the List, the value doesn't change on the grid until I click on the grid. Do you know what I am missing?

      O N 2 Replies Last reply
      0
      • A Andrew Stampor

        Thanks for your reply. I've changed the way I am trying to do this, but am not getting an update like I would like. What I am now doing is maintaining a List<CSomeObject>. That List is assigned as the datasource for the DataGridView. What I am noticing, though, is that when I update a value in one of the CSomeObjects that is part of the List, the value doesn't change on the grid until I click on the grid. Do you know what I am missing?

        O Offline
        O Offline
        Office Lineman
        wrote on last edited by
        #4

        Ahh, the List class implements IList, and not the IBindingList interface. IBindingList fires a ListChanged event that informs the BindingSource to which it is bound, and subsequently the DataGrid to which that BindingSource is bound, that it should update. IList fires no such event, so the BindingSource never knows that it should update itself. My solution to the same problem was to create a class that derives from System.Collections.CollectionBase and implements IBindingList, and use it in lieu of an IList-based List. -- I've killed again, haven't I?

        A 1 Reply Last reply
        0
        • A Andrew Stampor

          Thanks for your reply. I've changed the way I am trying to do this, but am not getting an update like I would like. What I am now doing is maintaining a List<CSomeObject>. That List is assigned as the datasource for the DataGridView. What I am noticing, though, is that when I update a value in one of the CSomeObjects that is part of the List, the value doesn't change on the grid until I click on the grid. Do you know what I am missing?

          N Offline
          N Offline
          Nougat H
          wrote on last edited by
          #5

          As far as the flickering goes, i think you should just enable double buffering. Here'a an example for a data grid: public class myDataGridClass : DataGrid { public myDataGridClass() { this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint , true); this.UpdateStyles(); } } Then, instead of using a DataGrid object you would simply use a myDataGrid object that has the DoubleBuffer flag set to true.

          1 Reply Last reply
          0
          • O Office Lineman

            Ahh, the List class implements IList, and not the IBindingList interface. IBindingList fires a ListChanged event that informs the BindingSource to which it is bound, and subsequently the DataGrid to which that BindingSource is bound, that it should update. IList fires no such event, so the BindingSource never knows that it should update itself. My solution to the same problem was to create a class that derives from System.Collections.CollectionBase and implements IBindingList, and use it in lieu of an IList-based List. -- I've killed again, haven't I?

            A Offline
            A Offline
            Andrew Stampor
            wrote on last edited by
            #6

            Hmmm... would this refresh only the changed cells in the grid, or does it refresh the whole thing anyway?

            O 1 Reply Last reply
            0
            • A Andrew Stampor

              Hmmm... would this refresh only the changed cells in the grid, or does it refresh the whole thing anyway?

              O Offline
              O Offline
              Office Lineman
              wrote on last edited by
              #7

              If I'm not mistaken, I believe it refreshes the row containing the element that was changed. -- I've killed again, haven't I?

              A 1 Reply Last reply
              0
              • O Office Lineman

                If I'm not mistaken, I believe it refreshes the row containing the element that was changed. -- I've killed again, haven't I?

                A Offline
                A Offline
                Andrew Stampor
                wrote on last edited by
                #8

                OK... I've changed the List to a BindingList, but when I make changes to the Properties, it doesn't update on the DataGridView. Is there a way I can trigger that event?

                O 1 Reply Last reply
                0
                • A Andrew Stampor

                  OK... I've changed the List to a BindingList, but when I make changes to the Properties, it doesn't update on the DataGridView. Is there a way I can trigger that event?

                  O Offline
                  O Offline
                  Office Lineman
                  wrote on last edited by
                  #9

                  There are probably a few ways to do it. My collection objects trigger the ListChanged event via a call to the collection class that contains them. -- I've killed again, haven't I?

                  A 1 Reply Last reply
                  0
                  • O Office Lineman

                    There are probably a few ways to do it. My collection objects trigger the ListChanged event via a call to the collection class that contains them. -- I've killed again, haven't I?

                    A Offline
                    A Offline
                    Andrew Stampor
                    wrote on last edited by
                    #10

                    Instead of creating my own implementation of the IBindingList interface, I used the BindingList class. When I was updating the properties of the objects in the list, it was not triggering the ListChanged event. When I looked at ListChanged in help, it mentioned that it will only be fired by items in the list that implement the INotifyPropertyChanged interface. I added INotifyPropertyChanged to my object class and had it trigger the PropertyChanged event when one of the properties I was interested in changed. This resolved both the issue with the grid not updating and the flickering issue I was having before. Thanks for all of your help. :-D You never mentioned the INotifyPropertyChanged interface, so if you haven't heard about it until now, I'd suggest giving it a look.

                    O 1 Reply Last reply
                    0
                    • A Andrew Stampor

                      Instead of creating my own implementation of the IBindingList interface, I used the BindingList class. When I was updating the properties of the objects in the list, it was not triggering the ListChanged event. When I looked at ListChanged in help, it mentioned that it will only be fired by items in the list that implement the INotifyPropertyChanged interface. I added INotifyPropertyChanged to my object class and had it trigger the PropertyChanged event when one of the properties I was interested in changed. This resolved both the issue with the grid not updating and the flickering issue I was having before. Thanks for all of your help. :-D You never mentioned the INotifyPropertyChanged interface, so if you haven't heard about it until now, I'd suggest giving it a look.

                      O Offline
                      O Offline
                      Office Lineman
                      wrote on last edited by
                      #11

                      Glad to be of help! I've never used INotifyPropertyChanged. It looks like that would get me the same effect as the manual way without having to register the collection class to the collected object. Learning is fun! :laugh: Thanks! -- I've killed again, haven't I?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups