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. Binding in-memory data to a DataGridView

Binding in-memory data to a DataGridView

Scheduled Pinned Locked Moved C#
5 Posts 3 Posters 6 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.
  • S Offline
    S Offline
    StevenS_Dev
    wrote on last edited by
    #1

    I want to bind an in-memory data class to a DataGridView using the DataSource member. I have read several ways of doing this and have implemented and tried several but once I set the DataSource, I then lose the ability to sort. I want to display my data class inside of a grid. I can use a List (List < MyDataClass >, for example) which works but I lose sorting. I can get sorting back, but I have to create my row like this object[] row = {MyDataClass.FirstName, MyDataClass.LastName}; which then removes the binding to the actual objects. I've now been trying to use DataSets and DataTables, but I still lose my binding. And the columns are not automatically generated from the exposed properties, like when I use a List collection. My next step is to create a class implementing the IBindingList interface. Will this be what I want? Any help or advice is greatly appreciated. Thanks. Oh, another wrench in the idea is that I want to flatten out the object. So my object may contain other objects which I want to also display in the table as part of the same row. I was thinking there was a way to bind a specific grid column to an object's property, but I haven't been able to find that yet.

    M 1 Reply Last reply
    0
    • S StevenS_Dev

      I want to bind an in-memory data class to a DataGridView using the DataSource member. I have read several ways of doing this and have implemented and tried several but once I set the DataSource, I then lose the ability to sort. I want to display my data class inside of a grid. I can use a List (List < MyDataClass >, for example) which works but I lose sorting. I can get sorting back, but I have to create my row like this object[] row = {MyDataClass.FirstName, MyDataClass.LastName}; which then removes the binding to the actual objects. I've now been trying to use DataSets and DataTables, but I still lose my binding. And the columns are not automatically generated from the exposed properties, like when I use a List collection. My next step is to create a class implementing the IBindingList interface. Will this be what I want? Any help or advice is greatly appreciated. Thanks. Oh, another wrench in the idea is that I want to flatten out the object. So my object may contain other objects which I want to also display in the table as part of the same row. I was thinking there was a way to bind a specific grid column to an object's property, but I haven't been able to find that yet.

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      When you say you lose the sorting on your List do you mean the ability to sort of the order you want loaded? I use a BindingSource as the DataSource for the DGV, but the data (from a SQL select) is loaded into the List in the order I want them displayed. So the sort is done before loasing the list. Any subsequent sorting is done by the user via the DGV. List<clsProduct> lProducts = oProd.GetRecordList(-1); dgData.DataSource = lProducts;

      Never underestimate the power of human stupidity RAH

      S 1 Reply Last reply
      0
      • M Mycroft Holmes

        When you say you lose the sorting on your List do you mean the ability to sort of the order you want loaded? I use a BindingSource as the DataSource for the DGV, but the data (from a SQL select) is loaded into the List in the order I want them displayed. So the sort is done before loasing the list. Any subsequent sorting is done by the user via the DGV. List<clsProduct> lProducts = oProd.GetRecordList(-1); dgData.DataSource = lProducts;

        Never underestimate the power of human stupidity RAH

        S Offline
        S Offline
        StevenS_Dev
        wrote on last edited by
        #3

        By losing the sort, I mean I can no longer sort when clicking on the column. This is just the nature of using a List or array as your datasource as they don't know how to sort per column. Also, when I say this is in-memory data, what I really mean is this is not connected to a database. I now have things working by deriving from IBindingList and implementing the sort routines as well as adding a Comparer, but it took me awhile. I tried How To Allow To Sort By Multiple Columns in Custom Data Binding which would not compile. It is based on an article from MSDN. So I fixed up the code to be like the MSDN article and, though it would compile, I still could not sort on my columns. I ended up having to use a sample in the MSDN Library for an IBindingList interface. But even THAT example does not show how to sort. So I took the necessary bits I needed from Implementing a strongly typed collection with sort/filter/GetChanges features and finally got things working.

        H 1 Reply Last reply
        0
        • S StevenS_Dev

          By losing the sort, I mean I can no longer sort when clicking on the column. This is just the nature of using a List or array as your datasource as they don't know how to sort per column. Also, when I say this is in-memory data, what I really mean is this is not connected to a database. I now have things working by deriving from IBindingList and implementing the sort routines as well as adding a Comparer, but it took me awhile. I tried How To Allow To Sort By Multiple Columns in Custom Data Binding which would not compile. It is based on an article from MSDN. So I fixed up the code to be like the MSDN article and, though it would compile, I still could not sort on my columns. I ended up having to use a sample in the MSDN Library for an IBindingList interface. But even THAT example does not show how to sort. So I took the necessary bits I needed from Implementing a strongly typed collection with sort/filter/GetChanges features and finally got things working.

          H Offline
          H Offline
          Henry Minute
          wrote on last edited by
          #4

          That sounds like an article to me. :)

          Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

          S 1 Reply Last reply
          0
          • H Henry Minute

            That sounds like an article to me. :)

            Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

            S Offline
            S Offline
            StevenS_Dev
            wrote on last edited by
            #5

            You are probably correct. I have now spent more time with things and have things working pretty well. I think I have the solution I was looking for.

            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