By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,362 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

Databinding to a function

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.