Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Can I bind DataGridView to my objects?
 

Can I bind DataGridView to my objects?

Hi,

Are there any scenarios that describe how to bind a DataGridView to your own Objects?

I've been to this useful website and there are no examples for this scenario: http://msdn2.microsoft.com/library/k39d6s23(en-us,vs.80).aspx)

I have a class that represents my data record - say it's a 'Person' object with name, phone, address etc - I want these objects to be represented as one row in a data grid.
If possible, I also want to bind them to text fields so that when someone clicks on a row, data is also shown in another (more easily editable) area with textboxes and so forth. I'd also really like to use the DataNavigator control because before I started, that's exactly what I thought I'd have to develop myself! It's very nice.

Actually, given the OO nature of the whole thing, I'm a bit preplexed as to why everyone is so keen to bind to relational data and not simply objects they already have in their class design.

In actual fact, I'm using an Object Database to store the data, but that needn't be known to anything else as it's completely transparent.

Now all the scenarios I can find seem to push me toward using a completely 'Unbound' DataGridView - adding rows and everything manually 'OR' they straight away start talking about relational SQL data.

It looks like I'm going to have to implement System.Collections.IList and maybe a few others.

Are there any examples of someone converting their custom class to implement these in such a way that allows binding to a DataGridView?

SpurryMoses  Wednesday, June 01, 2005 11:08 PM
Thanks very much.

With your tip on 'BindingList' I was able to find a useful blog via Google as well.
I thought I'd post a link to it since it has even more information. It was useful for me so I wanted to make sure it was recorded in this thread.


http://www.softinsight.com/bnoyes/PermaLink.aspx?guid=44feee48-9511-4324-a602-93811753027e

SpurryMoses  Thursday, June 02, 2005 5:20 AM

Probably the easiest way to do this is to put your "Person" instances in a BindingList<Person> (rather than create your own IList).  You'd then bind the BindingList<Person> to the DataGridView.  For example:


 BindingList<Person> blp = new BindingList<Person>();
 blp.Add(new Person());

 this.dataGridView1.DataSoruce = blp;
 



Joe

Joe Stegman  Thursday, June 02, 2005 3:34 AM
Thanks very much.

With your tip on 'BindingList' I was able to find a useful blog via Google as well.
I thought I'd post a link to it since it has even more information. It was useful for me so I wanted to make sure it was recorded in this thread.


http://www.softinsight.com/bnoyes/PermaLink.aspx?guid=44feee48-9511-4324-a602-93811753027e

SpurryMoses  Thursday, June 02, 2005 5:20 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView With Dataset - Changing Column Type To CheckBoxColumn
• creating an ODBC connection in VS.Net
• random appearance of datagridview coloumn in DataSet (even when visible is set to "False")
• DataGridView CellContentClick how do I get the row information for that cell click?
• Creating Update and Search buttons
• DataView problem after DataAdapter.Fill
• Problem with Databinding and Nested
• Best place to load ComboBoxCell in Datagridview
• Update Listview on another form
• Returning a Collection Class