Data Binding to C# class?

  • Thread starter Thread starter dm_dal
  • Start date Start date
D

dm_dal

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
 
--------------------
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

Hi Dave,

Typically, when you bind to a dataset you're binding to a 'collection' of data (multiple rows in a table). The scenario you are describing would basically be
binding to a single row in the table. In which case, the most efficient method would be to call a stored procedure with output parameters for the employee
record you want to populate your class with. For example, your stored procedure might look something like this...

create procedure getEmployeeData
@EmpID int,
@Firstname nvarchar(25) output,
@LastName nvarchar(25) output

as
select
@Firstname = [EmpFirstName],
@LastName = [EmpLastName]
from
EmpTable
where @EmpID = [EmpID]

Hope this helps,

--
Rick [MSFT]

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.
 
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
 
Back
Top