Michael
Tue Jul 29 11:14:48 CDT 2003
You can databind the datagrid to anything that implements IList.
CollectionBase implements IList. I have an example on SourceForge you can
look at. I've created a code generator that creates code for the 3 tiers of
a data access application (data, business logic, and UI). I don't claim
that it is the best for any application, but it has it's place. It creates
a class in the business layer (BLL) for each table which represents one
record. It also creates a strongly typed collection for each table to hold
instances of that class. I recommend binding that strongly typed collection
to the datagrid.
You can see the open source, freely downloadable code generator for this
at...
https://sourceforge.net/projects/dbobjecter
You can also create an array of any class and bind that array to the
datagrid!
At the July C#.NET user group in St. Louis I saw a great presentation on
databinding with datagrids. To see how to bind an array to a datagrid see
this sample project:
http://www.zrgwortz.com/datagrids.zip
This demonstrates binding to an ArrayList of item which is similar to
binding to a simple array. See "frmArrayBinding.cs".
======= Code =======================================
private void frmArrayBinding_Load(object sender, System.EventArgs e)
{
ArrayList al = new ArrayList();
al.Add(new String("test"));
al.Add(new String("test1"));
al.Add(new String("test2"));
al.Add(new String("test3"));
dg.DataSource=al;
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName="ArrayList";
DataGridTextBoxColumn cs = new DataGridTextBoxColumn();
cs.MappingName="Value";
cs.Width = 200;
cs.ReadOnly = true;
cs.HeaderText = "Value";
ts.GridColumnStyles.Add(cs);
dg.TableStyles.Clear();
dg.TableStyles.Add(ts);
}
======= End Code ====================================
Michael Lang
"dm_dal" <dmy75252@yahoo.com> wrote in message
news:u0dqtASVDHA.1748@TK2MSFTNGP12.phx.gbl...
> I know you can bind a dataset to a Windows.Forms.Control such as a
> comboBox .... but, was wondering if you can do the same to a class object?
>
> Example:
> Create a simple class (we'll call it clsEmployee)
> Create some properties of the employee like firstName, lastName etc...
> Create your SqlDataAdapter and DataSet and fill your SqlDataAdapter
> Create a DataViewManager and set it equal to the
DataSet.DefaultViewManager
>
> In a typical comboBox scenario, you would set you comboBox's "DataSource"
> to the DataViewManager and then set the comboBox's DisplayMember to
> (Table.FieldName) and the ValueMember to (Table.FieldName)
>
> I would like to be able to do this with my class properties:
>
> emp.firstName = (Table.FieldName);
> emp.lastName = (Table.FieldName);
>
> Can this be done? Obviously not through the means described above sind
> clsEmployee does not have a "DataSource" property, but is there another
way?
>
> dave
>
>