.NETGURU
Databinding to non-DataView/DataSet objects
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngescalate' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Dennis Werry
Hi,

I have posted this (twice) on aspngdatagridrepeaterdatalist and have
received no answers.

I'm attempting to bind datalists/datagrids/repeaters to my own classes. I
can get this to
work fine as long as I specify the fields to which to bind - (e.g.
AutoGenerateColumns="false"). I accomplish this by Implementing IEnumerable
and
creating a Default Property to return the appropriate field values. This all
works fine.

Although I'm not sure that I'd really want to do this in a production
environment, but to make my classes complete, and to better understand the
infrastructure of .NET, I'd like to implement the appropriate interface so
that I could do the equivalent of the following:

DataGrid1.AutoGenerateColumns = true;

// Employees ImplementsIEnumerable.
DataGrid1.DataSource = new Employees(Employees.NightShift);
DataGrid1.DataBind();

// Each Employee object has 17 fields, SSN, Lastname, Firstname, Address,
//JobClass, etc.

What do I have to do to my class so that the datagrid can discover the
number of "fields" and their "field names" at runtime? I'm sure I'm just
over-looking it, but I can't find anything that specifically describes that
process.

Said another way, "What interface or attribute does a DataView have that
allows a DataGrid to discover the field count, and field names at runtime?"

Like I said, the above works fine, if I AutoGenerateColumns="False" and then
specify the fields.

I've created another class that Implements IListSource that can return all
the fields in the objects. Additionally, I have its default property,
return the appropriate current data field value from the current row in the
datasource. I just don't know how to expose that class to the grid from my
objects.

Thanks in advance.

Dennis

Reply to this message...
 
    
Brian Bilbro (VIP)
I'm taking a totally wild stab at this, but perhaps implementing IDataReader
will solve this. IDataReader defines GetSchemaTable() which the DataBind
may use to get the header names. (there may a more appropriate interface to
implement, this is just a start)
--
Brian

----- Original Message -----
From: "Dennis Werry" <Click here to reveal e-mail address>
Sent: 03/19/2002 5:58 AM

[Original message clipped]

Reply to this message...
 
    
Dennis Werry
Thanks Brian,

I'm still hoping that somebody from Microsoft will give me a clue as to the
"BEST" way to handle this. But if they won't/can't/don't I will sure try
out the IDataReader Interface.

I just can't help but think that there is a more pure way to do that ...
since I don't think DataView implements the IDataReader Interface.

Thanks again.

Dennis

[Original message clipped]

Reply to this message...
 
    
Brian Bilbro (VIP)
I understand you wanting to get a Microsoft answer (you want to do it the
right way :). Having said that, AFAIK not only do some of these databind
look for some Interfaces (e.g. IList, ICollection, IEnumerable) but they
also do some inspection/reflection. It may be as simple as just adding the
method GetSchemaTable() (from IDataReader) or the property DataViewManager
(from DataView). Interesting, I just thumbed through the DataViewManager
class and saw that it implements the Interface IBindingList. That
definitely may be worth checking out.

Reference:
System.ComponentModel.IBindingList
http://msdn.microsoft.com/library/?url=/library/en-us/cpref/html/frlrfSystem
ComponentModelIBindingListClassTopic.asp?frame=true

Other neat references:
http://msdn.microsoft.com/library/?url=/library/en-us/cpref/html/frlrfSystem
ComponentModelBindableSupportClassTopic.asp?frame=true

http://msdn.microsoft.com/library/?url=/library/en-us/cpref/html/frlrfSystem
ComponentModelBindableSupportClassTopic.asp?frame=true

HTHs
Brian

----- Original Message -----
From: "Dennis Werry" <Click here to reveal e-mail address>
Sent: 03/19/2002 12:38 PM

[Original message clipped]

Reply to this message...
 
    
Isaac Hepworth
[Original message clipped]

ITypedList.

Cheers,

Isaac

[Original message clipped]

Reply to this message...
 
    
Dennis Werry
Isaac,

Thanks for the response. When does it get invoked and by what means?

In other words, does my Employees Class have to implement ITypedList? ...
or do I return a ITypedList from my default property? ... exactly how does
that work?

Thanks again.

Dennis

[Original message clipped]

Reply to this message...
 
    
Isaac Hepworth
[Original message clipped]

yes, your datasource has to implement ITypedList. this gets used from the
datagrid's private CreateAutoGeneratedColumns method.

the actual workings are that internally, your Employees class gets wrapped
in a PagedDataSource object---and it is this that the datagrid interacts
with. in DataGrid.CreateAutoGeneratedColumns, the datagrid calls
GetItemProperties (from ITypedList) on the PagedDataSource, and the
PagedDataSource checks if the datasource it's wrapping (your Employees
class) implements ITypedList. if it does, it delegates the call and the
columns of the grid get autogenerated from the result.

sorry if this hasn't come across clearly; email me off-list if you need more
detail or clarification,

isaac

[Original message clipped]

Reply to this message...
 
    
Dennis Werry
Isaac,

I got it to work ... several ways.

I return an object from my Employees.Item that:
Implements IEnumerable
Implements IBindingList
Implements IListSource

The Default Property Item(nIndex) of this returned object does not return
field names, but
Employee data. (this is what originally confused me)

yea!

But, however, no matter which way I get it to work, the columns come out in
seemingly random order ... not in the order of my enumerator. Any ideas
what is up with that?

I would assume that they would do something along the lines of:
for each o in oColumns.GetEnumerator()
    AddColumn(o ...)
next o

It is in very random order, not sorted, not in my order, I don't know what
order.

Thanks for your help.

Dennis

[Original message clipped]

Reply to this message...
 
    
shahzad
hi
After a long search i come to found your conversation at this forum
I want to implement same thing as you in C#
I have classes Depts & Dept

oDepts.GetAll();
foreach(Dept in oDepts)
{
response.write( Dept.Name);
response.write( Dept.Address);
response.write( Dept.Owner);
}
it works crrectly
now i want to implement it in such a way that i can assing myDepts object to a data grid
like this
dataGrid1.datasource = oDepts;
so that it automacitaly fill the datagrid
to achive this what is need to do
i have try to implemts different interface but not success

--------------------------------
From: shahzad arfan
Click here to reveal e-mail address
Reply to this message...
 
 
System.Collections.ICollection
System.Collections.IEnumerable
System.Collections.IList
System.ComponentModel.IBindingList
System.ComponentModel.IListSource
System.ComponentModel.ITypedList
System.Data.DataSet
System.Data.DataView
System.Data.DataViewManager
System.Data.IDataReader
System.Web.UI.WebControls.DataGrid
System.Web.UI.WebControls.PagedDataSource
System.Windows.Forms.DataGrid




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification