use of datagrid for non database project

  • Thread starter Thread starter Greg Williams
  • Start date Start date
G

Greg Williams

Can anyone tell me please if its possible to bind a datagrid to an array of
objects (defined by me) so that when i update the contents of the array, the
data grid updates its display.

Any tips would be greatly appreciated.

Greg.
 
You actually don't have to connect it to a database. All the data-aware controls can
be connected to disconnected datasets, which you can eumlate in code. It requires you
to create the correct type of list, however. I don't have any example handy, but you
should be able to find one pretty easy. Basically you create a dataset with:
DataSet dsCustomers = new DataSet();

Then do something like:

dgCustomers.DataSource = dsCustomers;
dgCustomers.DataMember = "Customers";

where dlgCustomers is the datagrid. The main problem you have is that a dataset can have multiple tables in it, and you need to create and fill just one of them. In this case you are telling the grid to use the "Customers" table in the dataset dsCustomers. I don't, off hand, remember how to fill the dataset in code. I know you will have to create the table within the dataset, and then fill it. Search help for dataset for starters.

-- Larry Maturo
 
Actually, you don't need a DataSet at all. Anything which implements
IList should suffice (such as ArrayList). The trick is that the objects
stored in the IList must have public properties for each column you want
display. If you want to use Column mapping, it gets trickier, but it can
be done. You need to use the name of the type of the collection (eg
"ArrayList") as you would the table name if it were a DataSet.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Thanks James and Larry.
I managed to get the thing working with just one line of code -
frightengly simple!

this.dgMyDataGrid.SetDataBinding(arrMyArrayOfObjects, null);

All my data was displayed correctly!

Thanks again.

Greg.
 
Greg said:
I managed to get the thing working with just one line of code -
frightengly simple!

this.dgMyDataGrid.SetDataBinding(arrMyArrayOfObjects, null);

Actually, it should be even simplier:

this.dgMyDataGrid.DataSource = arrMyArrayOfObjects;
 
Back
Top