المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Binding in-memory data to a DataGridView



C# Programming
04-02-2009, 10:55 PM
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.