Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Bind DataGridView to object with List<T> property
 

Bind DataGridView to object with List<T> property

Hello,
I have a business object with a couple of simple properties (string, bool) and 2 List<string> properties. I created a bindingsource to collect instances at runtime and then bound this to a DataGridView. I set AutoGenerateColumns to false and created all columns manually, also set all the DataPropertyName values. All columns are displayed properly, except:
I'd like to display the contents of the list properties of my object in DataGridviewComboBox columns created before, however, the dropdowns are all empty (the property within the bindingsource definitely is not).
How can I display my List<string> property contents residing in the bindingsource collection in the DropDown columns?
Thanks Andy
andy72  Friday, August 14, 2009 11:10 AM
You should be able to set the DataSource property of your combo column to your BindingSource's column of List<> type.
Michal Burger  Friday, August 14, 2009 11:23 AM
Hi andy,
With combobox
For this you have to write yourown code , like when user try to open combobox, set the datasource property of combobox to the list.

Instead of above , I would say try to paint cell ,
refer following link , it will give you start.
http://social.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq8

NareshG  Friday, August 14, 2009 12:00 PM

Hi,

The best method to set the default value is setting the value by handling DataGridView.DefaultValuesNeeded event.

void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)

{

e.Row.Cells["testcomboboxcolumn"].Value = "11";

}

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Friday, August 21, 2009 6:59 AM
You should be able to set the DataSource property of your combo column to your BindingSource's column of List<> type.
Michal Burger  Friday, August 14, 2009 11:23 AM
Hi andy,
With combobox
For this you have to write yourown code , like when user try to open combobox, set the datasource property of combobox to the list.

Instead of above , I would say try to paint cell ,
refer following link , it will give you start.
http://social.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq8

NareshG  Friday, August 14, 2009 12:00 PM
Thanks for your answers, actually, Michal's hint yields some progress:
I created two new BindingSources for the DropDownColumns out of the main BindingSource. I link the binding when my backgroundworker (fills bsData with my object instances) completes.

// bind				
this._dgData.DataSource = bsData;
BindingSource bsDGs = new BindingSource(bsData, "Desktops");
((DataGridViewComboBoxColumn)this._dgData.Columns["Desktops"]).DataSource = bsDGs;
BindingSource bsUsers = new BindingSource(bsData, "Users");
((DataGridViewComboBoxColumn)this._dgData.Columns["Users"]).DataSource = bsUsers;

Now, the following happens:
  1. While before both columns had completely empty drop downs (I had the first Binding statement only, see above), they now contain values, yippie!
  2. All initially displayed values (texts) are the same in each column over the rows (hmmmmm!?)
  3. If you drop the lists down there are all the correct different values that I need :-)
It seems to me that now I just have to set the first list value as the default display value...
Can I do that throuh some binding properties?

Thanks again!
  • Edited byandy72 Friday, August 14, 2009 12:35 PMformatting
  •  
andy72  Friday, August 14, 2009 12:34 PM

Hi,

The best method to set the default value is setting the value by handling DataGridView.DefaultValuesNeeded event.

void dataGridView1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)

{

e.Row.Cells["testcomboboxcolumn"].Value = "11";

}

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Friday, August 21, 2009 6:59 AM

You can use google to search for other answers

Custom Search

More Threads

• TableAdapter Configuration Wizard Question
• Long Serial Numbers.
• BindingList & Innherited Classes
• DateTimePiker into DataGridView
• How to display two columns in datagridviewcombobox
• Datagridviewcomboboxcolumn and virtual mode
• Concurrency Violation
• DataGrid printing tiny data
• How to use Datagridview in an n-tiered application
• DataGridView cell validation problem