You're welcome KF,
Glad that it is of assistance.
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| Reply-To: "(e-mail address removed)" <
[email protected]>
| From: <
[email protected]>
| References: <
[email protected]>
<
[email protected]>
<#
[email protected]>
<
[email protected]>
| Subject: Re: code sample wanted: creating and populating a collection of
objects from a datasource?
| Date: Wed, 17 Aug 2005 20:45:54 -0700
| Lines: 268
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <
[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: idea.urel.washington.edu 128.95.9.12
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:118790
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Super. Awesome. This is great. Thank you so much, Steven.
|
| Examples of this sort are surprisingly scarce: most online examples seem
| work directly against the data objects rather than dyamically populating
| custom objects as you've done here.
|
| Thanks again.
| -KF
|
| | > Hi KF,
| >
| > Since the question you mentioned is a very specific one, instead of
| > searching some separate articles, I've made a simple test page which
build
| > a dummy datatable and populate a "person" class's instance collection
from
| > the datatable and bind it to a datagrid for displaying, below is the
| > completet code. Also, I think you can try have a look in the www.
asp.net
| > or codeproject site to see whether there are any good examples or
articles
| > there.
| >
| > Hope helps,
| >
| > ========aspx============
| > <HTML>
| > <HEAD>
| > <title>objectPopulate</title>
| > <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
| > <meta name="CODE_LANGUAGE" Content="C#">
| > <meta name="vs_defaultClientScript" content="JavaScript">
| > <meta name="vs_targetSchema"
| > content="
http://schemas.microsoft.com/intellisense/ie5">
| > </HEAD>
| > <body>
| > <form id="Form1" method="post" runat="server">
| > <asp
ataGrid id="DataGrid1" runat="server"></asp
ataGrid>
| > </form>
| > </body>
| > </HTML>
| >
| >
| > ========code behind============
| > namespace DemoWebApp
| > {
| > public class objectPopulate : System.Web.UI.Page
| > {
| > protected System.Web.UI.WebControls.DataGrid DataGrid1;
| >
| > private void Page_Load(object sender, System.EventArgs e)
| > {
| > if(!IsPostBack)
| > {
| > DataGrid1.DataSource = GetPersons();
| > DataGrid1.DataBind();
| > }
| > }
| >
| >
| > private DataTable GetDataSource()
| > {
| > DataTable dt = new DataTable();
| >
| > dt.Columns.Add("id",typeof(long));
| > dt.Columns.Add("name",typeof(string));
| > dt.Columns.Add("age",typeof(int));
| > dt.Columns.Add("email",typeof(string));
| >
| > for(int i=0;i<10;i++)
| > {
| > DataRow dr = dt.NewRow();
| > dr[0] = i+1;
| > dr[1] = "Name_" + dr[0];
| > dr[2] = 25 + i;
| > dr[3] = dr[1] + "@test.org";
| >
| > dt.Rows.Add(dr);
| > }
| >
| > return dt;
| > }
| >
| > private Person[] GetPersons()
| > {
| > DataTable dt = GetDataSource();
| >
| > Person[] persons = new Person[dt.Rows.Count];
| >
| > for(int i=0;i<persons.Length;i++)
| > {
| > Person person = new Person();
| > person.Id = (long)dt.Rows
[0];
| > person.Name = (string)dt.Rows[1];
| > person.Age = (int)dt.Rows[2];
| > person.Email = (string)dt.Rows[3];
| >
| > persons = person;
| > }
| >
| > return persons;
| > }
| >
| > #region Web Form Designer generated code
| > override protected void OnInit(EventArgs e)
| > {
| > InitializeComponent();
| > base.OnInit(e);
| > }
| >
| > private void InitializeComponent()
| > {
| > this.Load += new System.EventHandler(this.Page_Load);
| >
| > }
| > #endregion
| > }
| >
| >
| > public class Person
| > {
| > long _id;
| > string _name;
| > int _age;
| > string _email;
| >
| > public long Id
| > {
| > get
| > {
| > return _id;
| > }
| > set
| > {
| > _id = value;
| > }
| > }
| >
| > public string Name
| > {
| > get
| > {
| > return _name;
| > }
| > set
| > {
| > _name = value;
| > }
| > }
| >
| > public int Age
| > {
| > get
| > {
| > return _age;
| > }
| > set
| > {
| > _age = value;
| > }
| > }
| >
| > public string Email
| > {
| > get
| > {
| > return _email;
| > }
| > set
| > {
| > _email = value;
| > }
| > }
| >
| >
| > public Person():this(0,"",20,"")
| > {
| > }
| >
| > public Person(long id, string name, int age, string email)
| > {
| > _id = id;
| > _name = name;
| > _age = age;
| > _email = email;
| > }
| > }
| > }
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
| > (This posting is provided "AS IS", with no warranties, and confers no
| > rights.)
| > --------------------
| > | Reply-To: "(e-mail address removed)" <[email protected]>
| > | From: <[email protected]>
| > | References: <[email protected]>
| > <[email protected]>
| > | Subject: Re: code sample wanted: creating and populating a collection
of
| > objects from a datasource?
| > | Date: Wed, 17 Aug 2005 15:17:19 -0700
| > | Lines: 44
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| > | X-RFC2646: Format=Flowed; Response
| > | Message-ID: <#[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: idea.urel.washington.edu 128.95.9.12
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:118752
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | Thank you very much, Steve, that's quite helpful.
| > |
| > | Does anyone have additional examples, or a C# example that I could
look
| > at?
| > |
| > | Thanks
| > | -KF
| > |
| > |
| > | | > | > This BarGraph web control contains a collection of Bar objects.
| > | > Full source code is included:
| > | > http://www.SteveOrr.net/articles/BarGraphs.aspx
| > | >
| > | > --
| > | > I hope this helps,
| > | > Steve C. Orr, MCSD, MVP
| > | > http://SteveOrr.net
| > | >
| > | >
| > | > | > | >> Trying to learn about manipulating collections of objects, and
| > populating
| > | >> these objects dynamically from datasources. Could someone post a
code
| > | >> sample that shows the following:
| > | >>
| > | >> Instantiating a collection object -- say, a dictionary.
| > | >> Populating that collection object with custom objects, say, Person.
| > What
| > | >> I really want to see is how to populate the properties of those
| > Person
| > | >> objects from a datasource: instantiate one Person, fill
| > Person.FirstName,
| > | >> Person.LastName, Person.ID with data, iterate to the next item in
the
| > | >> datasource, instantiate another person, fill Person.FirstName,
| > | >> Person.LastName...
| > | >>
| > | >> Any help out there?
| > | >>
| > | >> Thanks very much.
| > | >>
| > | >> -KF
| > | >>
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|