473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Databinding to a function

Nic
Hey,

Is it possible to do a databinding to a function, not to a property.

Ex. Object : Invoices - Invoice
Each Invoice has a collection of payments (an invoice can be paied in pieces) - payment
An invoice has also a function : getTotalPaid it returns a double.

Now I have a datagrid (ASP) I want to bind to several propertys of Invoice but I also want to show the getTotalPaid amount and the date of the last paiement.
Is that possible or do I have to make another custom class with a property TotalPaid and a property LastePaimentDate and bind that classe to my datagrid. I think it is a littelbit to much of classes.
tkx,
Nic

Nov 16 '05 #1
3 1953
Nic,

You can not data bind to a function. Like you said, creating a custom
class that exposes the values you want through properties would be the best
way to go.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nic" <Ni*@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
Hey,

Is it possible to do a databinding to a function, not to a property.

Ex. Object : Invoices - Invoice
Each Invoice has a collection of payments (an invoice can be paied in pieces) - payment An invoice has also a function : getTotalPaid it returns a double.

Now I have a datagrid (ASP) I want to bind to several propertys of Invoice but I also want to show the getTotalPaid amount and the date of the last
paiement. Is that possible or do I have to make another custom class with a property TotalPaid and a property LastePaimentDate and bind that classe to my
datagrid. I think it is a littelbit to much of classes. tkx,
Nic

Nov 16 '05 #2

Hi Nic,

What is going to be the source of the grid ?
If it's a collection of Invoices then you can do that in one of several
ways:

1- Use a method of the Invoice class to return the amount paid then you can
do something like this in your grid:
<asp:datagrid id=recordgrid runat="server" ShowHeader="false"
autoGenerateColumns="False" >
<columns>
<asp:templatecolumn ItemStyle-VerticalAlign="Middle" ItemStyle-Width="120"
ItemStyle-HorizontalAlign="left" >
<itemtemplate>
<span ><%# ((TSR)Container.DataItem).GetIDCode()%></span>
</itemtemplate>
</asp:templatecolumn>
I'm binding the grid agains a collection of type TSR, this class has a
method named GetIDCode() , you can do something similar just replace your
binding expression like this:
<span ><%# ((Invoice)Container.DataItem).GetTotalPaid().ToStr ing("C")
%></span>

The other way of doing it IF you don't want to change the Invoice class is
declare a method in the code behind that receive an Invoice instance and
return a string with the total paid.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Nic" <Ni*@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
Hey,

Is it possible to do a databinding to a function, not to a property.

Ex. Object : Invoices - Invoice
Each Invoice has a collection of payments (an invoice can be paied in pieces) - payment An invoice has also a function : getTotalPaid it returns a double.

Now I have a datagrid (ASP) I want to bind to several propertys of Invoice but I also want to show the getTotalPaid amount and the date of the last
paiement. Is that possible or do I have to make another custom class with a property TotalPaid and a property LastePaimentDate and bind that classe to my
datagrid. I think it is a littelbit to much of classes. tkx,
Nic

Nov 16 '05 #3
Hi Nic,

"Nic" <Ni*@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
Hi,
The source of the grid is Invoices (in reality Invoices is a collection object in the object Contract). Invoices consists of objects Invoice.
So you are ok :)

I tried what you proposed but at runtime he gives me an error :
CS0246: The type or namespace name 'Invoice' could not be found (are you missing a using directive or an assembly reference?)

Easy you have to include a reference to your assemblie , probably Invoices
is declared in another assembly ( dll ? )
Put this in the page:

<% @Import Namespace="Your.Assembly.Name.Here" %>

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
I don't understand why, the whole line in the grid is displaying data of the invoice object.
I have also a second question (struggeling whit datagrids) :
My Invoice object has properties, some of the properties are also an object ex. Code. The object Code has 2 properties : Id and Description.

In the datagrid I want to show my Invoice data and also Code.Description.
But following binding don't work
<asp:BoundColumn DataField="InvoiceId" HeaderText="Id"></asp:BoundColumn>
<asp:BoundColumn DataField="Code.description" HeaderText="Description"></asp:BoundColumn>
How can I bind to a property of a childobject.

Thanks for the help.
Nic

"Ignacio Machin ( .NET/ C# MVP )" wrote:

Hi Nic,

What is going to be the source of the grid ?
If it's a collection of Invoices then you can do that in one of several
ways:

1- Use a method of the Invoice class to return the amount paid then you can do something like this in your grid:
<asp:datagrid id=recordgrid runat="server" ShowHeader="false"
autoGenerateColumns="False" >
<columns>
<asp:templatecolumn ItemStyle-VerticalAlign="Middle" ItemStyle-Width="120" ItemStyle-HorizontalAlign="left" >
<itemtemplate>
<span ><%# ((TSR)Container.DataItem).GetIDCode()%></span>
</itemtemplate>
</asp:templatecolumn>
I'm binding the grid agains a collection of type TSR, this class has a
method named GetIDCode() , you can do something similar just replace your binding expression like this:
<span ><%# ((Invoice)Container.DataItem).GetTotalPaid().ToStr ing("C") %></span>

The other way of doing it IF you don't want to change the Invoice class is declare a method in the code behind that receive an Invoice instance and
return a string with the total paid.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Nic" <Ni*@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
Hey,

Is it possible to do a databinding to a function, not to a property.

Ex. Object : Invoices - Invoice
Each Invoice has a collection of payments (an invoice can be paied in

pieces) - payment
An invoice has also a function : getTotalPaid it returns a double.

Now I have a datagrid (ASP) I want to bind to several propertys of
Invoice but I also want to show the getTotalPaid amount and the date of the last
paiement.
Is that possible or do I have to make another custom class with a
property TotalPaid and a property LastePaimentDate and bind that classe to my
datagrid. I think it is a littelbit to much of classes.
tkx,
Nic


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dtblankenship | last post by:
Hello everyone, I know this question has been asked many times in the forums, and after spending a few days reading, I am still confused as to the answer. I have a ListBox (lstBox),...
3
by: Kevin Swanson | last post by:
I'm writing what should be a very simple app against an Oracle database. The app has a number of user controls, any one of which is loaded into a main display page using the loadControl method,...
3
by: John Bailey | last post by:
When I first built a few web pages in ASP .Net 2.0, I thought it was great. The formview and detailview contorls would automatically layout the controls for you, the update methods were...
2
by: Nathan Sokalski | last post by:
I am using a SortedList as my DataSource. However, one of the things I am using the data for is to generate the URL for the HyperLinks in my DataList. Therefore, I need to use the result of a...
8
by: GaryDean | last post by:
We have been noticing that questions on vs.2005/2.0 don't appear to get much in answers so I'm reposting some questions posted by some of the programmers here in our organization that never got...
1
by: Zak Lomakus | last post by:
Hi, From my main page (default.aspx) I am calling another .aspx (e.g. otherpage.aspx) page through Javascript using window.showModalDialog. otherpage.aspx has a databoud control (webchart),...
8
by: Dirk | last post by:
Hello, I have a problem to use databinding with my business layer classes. My data class does not have simple properties (string, int or datetime), instead, all my properties are objects of the...
1
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hello to all, I want to know if DataBinding in asp.net 2,0 is better than to fill up the values of the controls of the following form: this.miControlTextBox.Text = valorParaControlTextbox; ...
2
by: Nathan Sokalski | last post by:
I have a Repeater that uses a DataSource that has multiple fields. When the values of these fields is displayed in the Repeater, there are fields that are used in combination with other fields as...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.