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

[Solved] Convert XML string values before binding to grid

4 Answers 232 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mustafa Hussain
Top achievements
Rank 1
Mustafa Hussain asked on 22 Oct 2009, 03:03 PM
Hi I have some transactions that I am trying to bind to a radgrid. The transactions are being read from an XML file. I am using VB.NET and would like to change some of the values being bound from string to decimal.

Here is my code for the grid.
            <telerik:RadGrid ID="_pastItemsGrid" runat="server" AllowMultiRowSelection="true" 
                ShowFooter="true" AutoGenerateColumns="false" OnItemDataBound="_pastItemsGrid_ItemDataBound"
                <HeaderStyle Font-Bold="true" HorizontalAlign="Left" /> 
                <PagerStyle Mode="NextPrevAndNumeric" /> 
                <ClientSettings> 
                    <Selecting AllowRowSelect="true" /> 
                </ClientSettings> 
                <MasterTableView DataKeyNames="TranID"
                    <Columns> 
                        <telerik:GridBoundColumn HeaderText="Date" DataField="Date" /> 
                        <telerik:GridBoundColumn HeaderText="Description" DataField="Description" /> 
                        <telerik:GridBoundColumn HeaderText="Miles" DataField="Miles" DataType="System.Double" /> 
                        <telerik:GridBoundColumn HeaderText="Amount" DataField="Amount" DataType="System.Decimal" 
                            DataFormatString="{0:C2}" /> 
                        <telerik:GridClientSelectColumn HeaderText="Select" UniqueName="checkSelect" /> 
                    </Columns> 
                    <NoRecordsTemplate> 
                        <div> 
                            There were no records found.</div> 
                    </NoRecordsTemplate> 
                </MasterTableView> 
            </telerik:RadGrid> 
 

Here is the XML:
<Transcations> 
  <Transaction TranID="01"
    <Date>01/22/09</Date> 
    <Description>Target</Description> 
    <Miles>50400</Miles> 
    <Amount>504.46</Amount> 
  </Transaction> 
  <Transaction TranID="02"
    <Date>01/23/09</Date> 
    <Description>Ann Taylor Loft</Description> 
    <Miles>30500</Miles> 
    <Amount>305.50</Amount> 
  </Transaction> 
  <Transaction TranID="03"
    <Date>01/24/09</Date> 
    <Description>Dick's Sporting Goods</Description> 
    <Miles>5000</Miles> 
    <Amount>50.32</Amount> 
  </Transaction> 
</Transcations>

The grid is being bound with all the proper values but the miles value needs to be a long and the amount needs to be a decimal.

Here is how im binding the grid:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
        Dim ds As New DataSet 
        ds.ReadXml(Server.MapPath("../Transactions.xml")) 
        _pastItemsGrid.DataSource = ds 
        _pastItemsGrid.DataBind() 
    End Sub 
 

Now I tried the following to but it does not work.
    Protected Sub _pastItemsGrid_ItemDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles _pastItemsGrid.ItemDataBound 
        If TypeOf e.Item Is GridDataItem Then 
            Dim accrued As New Double 
            Dim amount As New Decimal 
            accrued = CDbl(e.Item.DataItem(2)) 
            e.Item.DataItem(2) = accrued 
            amount = CDec(e.Item.DataItem(3)) 
            e.Item.DataItem(3) = amount 
        End If 
    End Sub 

Can anyone please point me in the right direction? The end reason is that I would like to have totals for those two columns in the footer and the Aggregate="Sum" does not work if the bound item is of type string. Thanks

Mustafa

4 Answers, 1 is accepted

Sort by
0
Mustafa Hussain
Top achievements
Rank 1
answered on 22 Oct 2009, 04:28 PM
using VB.NET
Telerik.Web.UI ver 2009:2:701:20
and Visual Studio 2008 and .NET Framework 3.5
0
Tsvetoslav
Telerik team
answered on 28 Oct 2009, 10:28 AM
Hello Mustafa,

You need not do any conversion in code-behind - as the grid will take care of the fields' representation by itself. Attached is a sample demonstrating this.

Regards,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Mustafa Hussain
Top achievements
Rank 1
answered on 28 Oct 2009, 02:28 PM
Hey Tsvetoslav,

I am currently using the dataset.readxml to get the grid to bind. I still cannot format the amount by using the DataFormatString="{0:C2}" property. If the transaction amount returns 50.32 I want to display it in the grid as $50.32

I do have the grid working and am im using a GridTemplateColumn and formatting the string in code behind for one of the columns.

Here is what the columns look like currently:
                    <Columns> 
                        <telerik:GridBoundColumn HeaderText="Date" DataField="Date" /> 
                        <telerik:GridBoundColumn HeaderText="Description" DataField="Description" /> 
                        <telerik:GridBoundColumn HeaderText="Amount" DataField="Amount" UniqueName="Amount" /> 
                        <telerik:GridTemplateColumn HeaderText="Reward Value" UniqueName="Value"
                            <ItemTemplate> 
                                <div style="color: #0DAE4B;"
                                    <asp:Label ID="_milesLabel" runat="server" Text='<%# GetCurrency(Eval("Miles")) %>' /> 
                                </div> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                        <telerik:GridTemplateColumn UniqueName="checkSelect"
                            <HeaderTemplate> 
                                <asp:CheckBox ID="_allCheck" runat="server" AutoPostBack="true" OnCheckedChanged="ToggleAll" /> 
                            </HeaderTemplate> 
                            <ItemTemplate> 
                                <asp:CheckBox ID="_rowCheck" runat="server" AutoPostBack="true" OnCheckedChanged="ToggleCheck" /> 
                            </ItemTemplate> 
                        </telerik:GridTemplateColumn> 
                    </Columns> 
 

So now if the value column in the xml is 5000 the grid displays that as 5,000 Points. I am doing this in the code behind. I would like to avoid this if possible for the Amount column.

My Code Behind:
    Public Function GetCurrency(ByVal currencyRaw As Object) As String 
        Dim currencyToShow As String 
        Dim currencyInt As New Integer 
        currencyInt = CInt(currencyRaw) 
        currencyToShow = String.Format("{0:#,##}", currencyInt) 
        Return currencyToShow & " " & Session.Currency 
    End Function 
 
    Protected Sub _pastItemsGrid_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles _pastItemsGrid.NeedDataSource 
        Dim ds As New DataSet 
        ds.ReadXml(Server.MapPath("~/Transactions.xml")) 
        _pastItemsGrid.DataSource = ds 
    End Sub 
 

I know I can apply the same logic as above to get a properly formatted amount string but I was hoping there was a way around that. Thanks again.
0
Tsvetoslav
Telerik team
answered on 30 Oct 2009, 01:57 PM
Hello Mustafa,

I have prepared a small sample that demonstrates how to achieve the desired conversion. Please, find it attached to this post.

Greetings,
Tsvetoslav
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Mustafa Hussain
Top achievements
Rank 1
Answers by
Mustafa Hussain
Top achievements
Rank 1
Tsvetoslav
Telerik team
Share this question
or