Creating Total Cell in DataGrid not working with Template Field

  • Thread starter Thread starter daz_oldham
  • Start date Start date
D

daz_oldham

(Using C# 2.0)

Hi

I was trying to get a field formatted as currency, but could not do
this in my DataGrid unless I did the field as a template field.

However, when I try to create a total row when the row is databound, I
am just getting back that the value is a zerol length string -
presumably because my value is in a label.

How do I get around this? Given that unles the value is in the
template field, for some reason it won't write the £ sign out.

Many thanks

Darren


Source code:
<asp:TemplateField HeaderText="£ Per Night">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("PricePerNight") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#
Bind("PricePerNight", "{0:c}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>


Code Behind:
protected void dgRoomBreakdown_ItemDatabound(object sender,
GridViewRowEventArgs e)
{

switch (e.Row.RowType)
{
case DataControlRowType.DataRow:

if (e.Row.Cells[4].Text.Length > 0)
{
dPricePerNight +=
Double.Parse(e.Row.Cells[4].Text);
}
break;
case DataControlRowType.Footer:
e.Row.Cells[4].Text = dPricePerNight.ToString();
break;

}
}
 
Fixed it myself!

You basically put the value into a text box (see code below) and then
do something like the following...

Daz

switch (e.Row.RowType)
{
case DataControlRowType.DataRow:


TextBox txtVal = (TextBox)(e.Row.Cells[4].Controls[1]);
if (txtVal.Text.Length > 0)
{
dPricePerNight +=
Double.Parse(txtVal.Text.Replace("£",""));
}
break;
case DataControlRowType.Footer:
e.Row.Cells[4].Text = dPricePerNight.ToString();
break;

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top