PC Review


Reply
Thread Tools Rate Thread

Data Binding to C# class?

 
 
dm_dal
Guest
Posts: n/a
 
      28th Jul 2003
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


 
Reply With Quote
 
 
 
 
Rick Rainey[MSFT]
Guest
Posts: n/a
 
      29th Jul 2003
--------------------
>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.

 
Reply With Quote
 
Michael Lang
Guest
Posts: n/a
 
      29th Jul 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
100K item data binding: Is asynchronous data binding possible? R Reyes Microsoft C# .NET 14 5th Feb 2008 09:47 PM
Parse method never called when data binding from class property to textedit Richard Urwin Microsoft C# .NET 3 15th Oct 2004 04:42 AM
Parse method never called when data binding from class property to textedit Richard Urwin Microsoft Dot NET Framework Forms 1 14th Oct 2004 09:43 AM
Binding to custom data class Bill Youngman Microsoft ADO .NET 1 5th Jul 2004 12:39 PM
Data-binding ComboBox to a custom class doesn't work Tim A. Microsoft Dot NET Framework Forms 0 9th Jun 2004 05:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:15 AM.