This is a migrated thread and some comments may be shown as answers.

Displaying Column Total

6 Answers 148 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Phil
Top achievements
Rank 1
Phil asked on 29 Dec 2012, 04:24 PM
I want to display the total of a single column in my radgrid(preferably in the footer), anytime the user deletes an item from the grid the total value should be updated automatically, there are similar posts on this but they are to complex for me i'm new to this.  This is my code :
<telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="0"
    GridLines="None" AllowSorting="True" AutoGenerateColumns="False"
        Skin="WebBlue">
<ClientSettings>
<Selecting CellSelectionMode="None" AllowRowSelect="True"></Selecting>
</ClientSettings>
 
<MasterTableView AllowAutomaticDeletes="True" ShowFooter="True">
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
 
<RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
 
    <Columns>
        <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn column"
            UniqueName="TemplateColumn">
            <itemTemplate>
                <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Image") %>' Height="100px" Width="85px"/>
            </itemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn1 column"
            UniqueName="Title" HeaderText="Title">
            <itemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Title") %>'></asp:Label>              
            </itemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn2 column"
            UniqueName="Price(GHc)" HeaderText="Price(GHc)">
            <itemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Price") %>'></asp:Label>              
            </itemTemplate>
            <FooterTemplate>
                <asp:TextBox runat="Server" ID="TotalTxt">
                </asp:TextBox>
            </FooterTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridClientDeleteColumn FilterControlAltText="Filter column column"
            ImageUrl="mvwres://Telerik.Web.UI.Skins, Version=2012.1.215.40, Culture=neutral, PublicKeyToken=121fae78165ba3d4/Telerik.Web.UI.Skins.WebBlue.Grid.Delete.gif"
            UniqueName="column">
        </telerik:GridClientDeleteColumn>
    </Columns>
 
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
</MasterTableView>
 
<FilterMenu EnableImageSprites="False"></FilterMenu>
</telerik:RadGrid>

6 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 31 Dec 2012, 04:23 AM
Hi Phil,

After inspecting your code I guess you need to show the sum of a column(GridTemplatecolumn) in the Footer Template TextBox. Please check the following code snippet.

ASPX:
<telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn2 column"
    UniqueName="Price(GHc)" HeaderText="Price(GHc)">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <asp:TextBox runat="Server" ID="TotalTxt">
        </asp:TextBox>
    </FooterTemplate>
</telerik:GridTemplateColumn>

C#:
int total;
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        Label Label1 = (Label)dataItem.FindControl("Label6");
        int fieldValue = Convert.ToInt32(Label1.Text);
        total += fieldValue;
    }
    if (e.Item is GridFooterItem)
    {
        GridFooterItem footerItem = e.Item as GridFooterItem;
        TextBox txt = (TextBox)footerItem.FindControl("TotalTxt");
        txt.Text = "total: " + total.ToString();
    }
}

Thanks,
Shinu.
0
Phil
Top achievements
Rank 1
answered on 31 Dec 2012, 04:22 PM
"Multiple controls with the same ID 'Label1' were found. FindControl requires that controls have unique IDs." - This is the error i got when i tried running the code.  i understand the error but it's in a grid view so i really do not know how to fix it.
0
Elliott
Top achievements
Rank 2
answered on 31 Dec 2012, 05:34 PM
you get the message that Label1 appears more than once - because Label1 appears more than once
rename either the Label1 in the Footer or the Label1 in the GridTemplate
0
Shinu
Top achievements
Rank 2
answered on 02 Jan 2013, 12:10 PM
Hi,

The error is due to the same ID for the labels in the two GridTemplateColumn. Please try different IDs for the Labels.

ASPX:
<telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn1 column"
    UniqueName="Title" HeaderText="Title">
    <itemTemplate>
           <asp:Label ID="Label1" runat="server" Text='<%# Eval("Title") %>'></asp:Label>             
    </itemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn2 column"
    UniqueName="Price(GHc)" HeaderText="Price(GHc)">
    <ItemTemplate>
        <asp:Label ID="Label2" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
        <asp:TextBox runat="Server" ID="TotalTxt">
        </asp:TextBox>
    </FooterTemplate>
</telerik:GridTemplateColumn>

Thanks,
Shinu.
0
Phil
Top achievements
Rank 1
answered on 02 Jan 2013, 03:34 PM
Thanx alot that worked well but now the problem i am facing is that anytime i delete a row the total is not updated, i use a datable as the datasource so when i refresh the page all the deleted items return and the total still remains the same.  Can you please help me fix this.
0
Elliott
Top achievements
Rank 2
answered on 02 Jan 2013, 04:05 PM
Phil - don't be insulted with this but... deleting a row in the data table is only half the task - you need to go and delete the item from the data source itself
Telerik automatically triggers a NeedDataSource event so if you haven't issued a DELETE to the underlying data source (i.e. SQL table) then all your changes will be wiped out
if you have, it must not be working

thanks, Shinu, for expanding on all my most recent posts
Tags
Grid
Asked by
Phil
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Phil
Top achievements
Rank 1
Elliott
Top achievements
Rank 2
Share this question
or