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

Xml datasource and grid column Aggregate

2 Answers 132 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Amit
Top achievements
Rank 1
Amit asked on 11 Oct 2011, 10:00 PM
Hi, I am using XmlDataSource with RadGrid. I need to use Column's Aggregate functionality to show sum (in footer) in one column. As datasource is XML, grid considers I have all string data even though I have numbers in the column. How can I use Sum Aggregate functionality with Xml data source?

code sample:
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/data.xml"  XPath="/root/Row"></asp:XmlDataSource>
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="XmlDataSource1" ShowFooter="true">
    <MasterTableView AutoGenerateColumns="False" DataSourceID="XmlDataSource1">
        <Columns>
          <telerik:GridBoundColumn DataField="col1" HeaderText="Product" SortExpression="col1" Aggregate="Count" ...

Thank you,
Amit Vaishnav

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 Oct 2011, 08:03 AM
Hello Amit,

You can try the following code snippet.

ASPX:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="XmlDatasource_Aggregate.aspx.cs"
    Inherits="Radgrid_XmlDatasource_Aggregate" %>
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="scriptmngr" runat="server">
        </asp:ScriptManager>
        <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="XmlDataSource1" AutoGenerateColumns="false"
            ShowFooter="true" OnItemDataBound="RadGrid1_ItemDataBound">
            <MasterTableView>
                <Columns>
                    <telerik:GridBoundColumn DataField="Name" UniqueName="Name" HeaderText="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Author" UniqueName="Author" HeaderText="Author">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Number" UniqueName="Number" HeaderText="Number">
                    </telerik:GridBoundColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Radgrid/XMLFile.xml">
        </asp:XmlDataSource>
    </div>
    </form>
</body>
</html>


C#:
int total;
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
  {
    if (e.Item is GridDataItem)
     {
       GridDataItem dataItem = e.Item as GridDataItem;
       String s = dataItem["UniqueColumnName"].Text;
        int t = int.Parse(s);
        total += t;
     }
    if (e.Item is GridFooterItem)
     {
        GridFooterItem footerItem = e.Item as GridFooterItem;
        footerItem["UniqueColumnName"].Text = "Total: " + total.ToString();
      }
  }

Another approach is you can try to change the DataType on the DataColumn of the DataTable before binding to the grid.

Thanks,
Princy.
0
Amit
Top achievements
Rank 1
answered on 12 Oct 2011, 02:21 PM
Hello Princy,

Thanks for reply. First approach worked for getting total. But I want grid to treat column value as number. So other functionality like number formatting will work. Right now, I have currency format on the column, I get error.

in 2nd approach: I don't have DataSet; I have xml string which cannot be serialized in DataSet.

Thank you,
Amit
Tags
Grid
Asked by
Amit
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Amit
Top achievements
Rank 1
Share this question
or