Showing datagrid totals in the footer

G

Guest

I am trying to figure out how to display the sum total of a numeric column in
the footer of a datagrid. I have tried putting a label control in the footer
of a column but for some reason when I try to reference the label control
using:

txtTotalDollars.Text = ttlDollars.ToString();

I get an error saying that the label control txtTotalDollars does not exist.
Note that when I place the label outside of the footer it works fine.

Any ideas?

Thanks, Justin.
 
G

Guest

If you add any controls inside datagrid (either in footer or header). it will
be placed under controls collection of datagrid. So if you try to directly
access the control, you will get error like this. If you want to set some
value to that label or if you want to access that label. then you need to do
this

datagriditem.findcontrol("Labelname")

Check out this article for more info,
http://www.extremeexperts.com/Net/Articles/AddingControlstoFooter.aspx
 
G

Guest

Can you or someone else ellaborate a little more on how to accomplish this
task? The article was of little help.

I have this so far: DataGrid1.FindControl("txtTotalDollars"); //
txtTotalDollars is the label

I am unable to asign a value to that code. How can I do that?

Thanks, Justin.
 
S

Scott Allen

Hi Justin:

FindControl has to be called on the DataGridItem that contains the
textbox. The DataGridItem you want is the footer, and probably the
last one in the Item array

DataGrid.Items[DataGrid.Items.Count-1].Findcontrol("txtTotalDollars");

For more info:
http://odetocode.com/Articles/116.aspx
 
G

Guest

Ok, I seem to have the correct code but for some reason nothing is showing up
in the label.

Here is my code:

protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
Label lblDollars = (Label)e.Item.FindControl("txtTotalDollars");
lblDollars.Text = ttlDollars.ToString();
}

Any ideas?

Thanks, for the help.

Justin.
 
S

Scott Allen

Hi Justin:

That snippet of code looks entirely reasonable. Have you set a
breakpoint with the deubgger? Any chance of ttlDollars being an empty
string?
 
G

Guest

I tested ttlDollars on another non datagrid label and it works fine, so the
variable is not empty, I must be using findcontrol on the wrong item.
 
G

Guest

Ok, I think I found the solution. It is in the way I am wiring up the event,
heres what I am doing now:

this.DataGrid1 += DataGridItemEventArgs(this.DataGrid1_ItemDataBound);

I can't think of any other way to wire it up. Any suggestions?

Thanks, Justin.
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top