Show collection of objects in grid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael C

    Show collection of objects in grid

    I wish to show a collection of objects in the DataGridView control. This is
    working fairly well except in one particular case. If my collection starts
    empty when I assign it to the grid then when an item is added, the item
    appears but every cell in the grid is blank. I am assigning the collection
    to the grid like this:

    MyGrid.DataSour ce = MyCollection

    This works great if the collection contains 1 or more items but in the case
    that the user is creating a new collection from scratch then it does not
    work. I can see what is happening, the grid doesn't know which columns to
    bind to which properties of my object if the collection has not objects. So
    far I've managed to get around this by rebinding my collection to the grid
    when the first item is added but this seems like a hack. Anyone have a
    better solution?

    BTW, I create the columns in the grid at design time and have set
    AutoGenerateCol umns to false.

    Thanks,
    Michael


  • Marc Gravell

    #2
    Re: Show collection of objects in grid

    What is MyCollection?

    WinForms uses various approaches for obtaining metadata (i.e. columns):

    [don't worry about these first two; you don't need to know them...]
    * if the source implements IListSource, the IList is obtained
    * if the source implements ITypedList, this is queried for metadata

    then:
    * if the source implements IList and has a typed (non-object) indexer
    "SomeType this[int]", then the "SomeType" is inspected
    * if the source implements IList and is non-empty, the first item is
    obtained and inspected
    * if the source doesn't implement IList, it might (depending on the
    control) get used as a single row
    * otherwise you get nada

    If you use List<T>, Collection<T>, etc you automatically get the typed
    indexer, so this is the simplest fix. Don't use ArrayList.

    Marc

    Comment

    • Michael C

      #3
      Re: Show collection of objects in grid

      "Marc Gravell" <marc.gravell@g mail.comwrote in message
      news:OYG5N7AQJH A.444@TK2MSFTNG P05.phx.gbl...
      What is MyCollection?
      It implements IBindingList but it is my collection so I can do as I need.
      WinForms uses various approaches for obtaining metadata (i.e. columns):
      >
      [don't worry about these first two; you don't need to know them...]
      * if the source implements IListSource, the IList is obtained
      * if the source implements ITypedList, this is queried for metadata
      >
      then:
      * if the source implements IList and has a typed (non-object) indexer
      "SomeType this[int]", then the "SomeType" is inspected
      * if the source implements IList and is non-empty, the first item is
      obtained and inspected
      * if the source doesn't implement IList, it might (depending on the
      control) get used as a single row
      * otherwise you get nada
      >
      If you use List<T>, Collection<T>, etc you automatically get the typed
      indexer, so this is the simplest fix. Don't use ArrayList.
      I'll give it a go, it can't hurt to implement another interface :-)

      Thanks for your help,
      Michael


      Comment

      • Marc Gravell

        #4
        Re: Show collection of objects in grid

        Indeed. In that case, it should just need IList plus a typed indexer.

        Comment

        • Michael C

          #5
          Re: Show collection of objects in grid

          "Marc Gravell" <marc.gravell@g mail.comwrote in message
          news:83169fea-75ed-4603-a7b6-175ac110b1b6@a1 7g2000prm.googl egroups.com...
          Indeed. In that case, it should just need IList plus a typed indexer.
          Just IList on its own is no good because you can't raise an event to tell
          the grid that items have been added/removed/changed. IBindingList has such
          an event (I can't remember what it is called but it's only got 1 event).

          Michael


          Comment

          Working...