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