Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView.DataSource property and Generics
 

DataGridView.DataSource property and Generics

Here's the scenario i am working with...

//works ok, gridview displays data in customersNonGeneric object.

SortedList CustomersNonGeneric; //holds Customer objects

myDataGridView.DataSource = CustomersNonGeneric.Values;

myDataGridView.Columns["col1"].DataPropertyName = "FName";

myDataGridView.Columns["col2"].DataPropertyName = "LName";

When i converted the above to use Generics, no data is displayed...

//does NOT throw any errors but GridView does not show data, as if CustomersGeneric was empty, which it is not.

SortedList<DateTime, Customer> CustomersGeneric;

myDataGridView.DataSource = CustomersGeneric.Values;

myDataGridView.Columns["col1"].DataPropertyName = "FName";

myDataGridView.Columns["col2"].DataPropertyName = "LName";

My workaround was to covert the IList<> into a List<> as follows:

IList<Customer> CustomersIList = (IList<Customer>)CustomersGeneric.Values;

List<Customer> Customers = new List<Customer>();

foreach (Customer cust in CustomersIList) {

Customers.Add(cust);

}

myDataGridView.DataSource = Customers;

myDataGridView.Columns["col1"].DataPropertyName = "FName";

myDataGridView.Columns["col2"].DataPropertyName = "LName";

//etc..

Can anyone please explain:

1. Why is List<> acceptable in GridView.DataSource,but IList<> is not?

2. My workaround works, but is there a better way of doing it - or am i already doing it right?

cheers.
joedotnet  Friday, February 22, 2008 4:07 AM

Hi joedotnot,

Here are the answers to your questions:

1. The DataGridView control supports the standard Windows Forms data binding model, so it will bind to instances of classes described in the following list:

· Any class that implements the IList interface, including one-dimensional arrays.

· Any class that implements the IListSource interface, such as the DataTable and DataSet classes.

· Any class that implements the IBindingList interface, such as the BindingList<(Of <(T>)>) class.

· Any class that implements the IBindingListView interface, such as the BindingSource class.

IList doesn’t implement any of the interfaces described above, that’s the reason that your DataGridView doesn’t display anything.

Here’s an document describes DataGridView’s required interfaces

DataGridView Control Overview (Windows Forms)

Here’s an document describes interfaces implemented by IList

IList(T) Generic Interface (System.Collections.Generic)

2. Another approach is

Code Snippet

myDataGridView.DataSource = CustomersIList.ToList();

The ToList() method returns the internal objects in a List object, the List class implements IList interface.

Best regards,

Jacob

Jacob Sui - MSFT  Thursday, February 28, 2008 5:48 AM

The datagridview needs a datasource that implements the IList interface not IDictionary. I would use a BindingList<T> instead of a List<T> because it is setup for databinding

Ken Tucker  Friday, February 22, 2008 8:29 AM

Thanks for the reply Ken... but if you refer to my non-working example above, itclearly implements the IList interface.

myDataGridView.DataSource = CustomersGeneric.Values;

i.e. CustomersGeneric implements IDictionary but CustomersGeneric.Values implements IList.

joedotnet  Friday, February 22, 2008 10:23 AM

Hi joedotnot,

Here are the answers to your questions:

1. The DataGridView control supports the standard Windows Forms data binding model, so it will bind to instances of classes described in the following list:

· Any class that implements the IList interface, including one-dimensional arrays.

· Any class that implements the IListSource interface, such as the DataTable and DataSet classes.

· Any class that implements the IBindingList interface, such as the BindingList<(Of <(T>)>) class.

· Any class that implements the IBindingListView interface, such as the BindingSource class.

IList doesn’t implement any of the interfaces described above, that’s the reason that your DataGridView doesn’t display anything.

Here’s an document describes DataGridView’s required interfaces

DataGridView Control Overview (Windows Forms)

Here’s an document describes interfaces implemented by IList

IList(T) Generic Interface (System.Collections.Generic)

2. Another approach is

Code Snippet

myDataGridView.DataSource = CustomersIList.ToList();

The ToList() method returns the internal objects in a List object, the List class implements IList interface.

Best regards,

Jacob

Jacob Sui - MSFT  Thursday, February 28, 2008 5:48 AM
I used following solution to convert to arraylist and then bind it to datagridview...

Private Function Collectiontoarraylist(ByVal inar As System.Collections.Generic.SortedList(Of Double, guitar)) As ArrayList
Dim outar As New ArrayList
For Each val As KeyValuePair(Of Double, guitar) In inar
outar.Add(val.Value)
Next
Return outar
End Function



strange enough i could not find .toList() so currently I am using above method if someone knows where exactly is .tolist () I could save huge loops ???
YeleBatha  Thursday, May 21, 2009 11:27 AM
@YeleBatha: in case you didn't find it yet:

make sure your targetting against .NET 3.5 Framework and add

C#:
  using System.Linq;
VB.NET:
  Imports System.Linq
in your class file.
DaddyCool  Thursday, August 20, 2009 8:49 AM

You can use google to search for other answers

Custom Search

More Threads

• binding a TextBox to a column in a dataGrid
• how to print a dataGridView
• dataGridView + mousewheels
• i want to do this thing but there is some problem
• "object reference not set to an instance of an object" error message
• prepare a messenger project how to do
• Refresh DataSource of DataGridView Inside ToolStripDropDown
• Why DataGrid cell focus chg force DataTable.ColumnChanging event?
• DataGridView unbound column not updated.
• How to using datagrid add subtotal and grandtotal