0

I am trying to change the data in my repeater so far I am cycling through each row by Item and AlternatingItem but I want change the values in the last column so that the value is to two decimal places. I have a basic Connection to a stored procedure which populates the repeater code below:

connection to the stored procedure:

    SqlDataAdapter da = new SqlDataAdapter("Stored Procedure", conn);
    da.SelectCommand.CommandType = CommandType.StoredProcedure;
    da.SelectCommand.Parameters.Add(new SqlParameter("@Admin", "ALL"));
    DataSet dataset = new DataSet();
    da.Fill(dataset);
    rptItems.DataSource = dataset.Tables[0];
    rptItems.DataBind();

Code for the ItemDataBound method:

    protected void rptconsole_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType  ==ListItemType.AlternatingItem)
        {
           //Code should go here which changes the values of the last column
           //To two decimal places
        }
    }

html/XAML code to populate the repeater:

<asp:Repeater ID="rptconsole" runat="server" OnItemDataBound="rptconsole_ItemDataBound">

       <HeaderTemplate>

            <table id="tablework">

                <th>Console</th>
                <th>Color</th>
                <th>Features</th>
                <th>Description</th>
                <th>price</th>

</HeaderTemplate>
        <ItemTemplate>

            <tr>
                <td align="center"><%# Eval("[ConsoleType]") %></td>
                <td align="center"><%# Eval("[color]") %></td>
                <td align="center"><%# Eval("[features]") %></td>
                <td align="center"><%# Eval("[desc]") %></td>
                <td align="center"><%# Eval("[price]") %></td>
            </tr>


        </ItemTemplate>
        <AlternatingItemTemplate>

            <tr>
        <td align="center"><%# Eval("[ConsoleType]") %></td>
                <td align="center"><%# Eval("[color]") %></td>
                <td align="center"><%# Eval("[features]") %></td>
                <td align="center"><%# Eval("[desc]") %></td>
                <td align="center"><%# Eval("[price]") %></td>
            </tr>
        </AlternatingItemTemplate>

        <FooterTemplate>
            </table>
        </FooterTemplate> 
    </asp:Repeater>
user3668266
  • 173
  • 1
  • 3
  • 15

2 Answers2

0

change HTML like i did, I made the td to runat server

   <HeaderTemplate>

        <table id="tablework">

            <th>Console</th>
            <th>Color</th>
            <th>Features</th>
            <th>Description</th>
            <th>price</th>

        <tr>
            <td align="center"><%# Eval("[ConsoleType]") %></td>
            <td align="center"><%# Eval("[color]") %></td>
            <td align="center"><%# Eval("[features]") %></td>
            <td align="center"><%# Eval("[desc]") %></td>
            <td  runat="server" id="price" align="center"><%# Eval("[price]") %></td>
        </tr>


    </ItemTemplate>
    <AlternatingItemTemplate>

        <tr>
    <td align="center"><%# Eval("[ConsoleType]") %></td>
            <td align="center"><%# Eval("[color]") %></td>
            <td align="center"><%# Eval("[features]") %></td>
            <td align="center"><%# Eval("[desc]") %></td>
            <td  runat="server" id="price" align="center"><%# Eval("[price]") %></td>
        </tr>
    </AlternatingItemTemplate>

    <FooterTemplate>
        </table>
    </FooterTemplate> 
</asp:Repeater>

After that you need to find that td at server side and need to change the value like i did below

protected void rptconsole_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType  ==ListItemType.AlternatingItem)
        {
           HtmlTableCell tdPrice = e.Item.FindControl("price") as HtmlTableCell;    
           tdPrice.InnerText = Convert.ToDecimal(tdPrice.InnerText).ToString("C2"); //2dp Number or any value you want
        }
    }

ref: Formatting a float to 2 decimal places

Community
  • 1
  • 1
Vijay Singh Rana
  • 1,060
  • 14
  • 32
  • thanks for the response but the tdPrice.InnerText=.... I don't want to change it by hard code I want to change the number which is already in that column to two decimal places – user3668266 Jun 13 '14 at 11:58
0

You don't need to use OnItemDataBound event for that at all.

You can do it like this in ASP markup:

...
<td  runat="server" id="price" align="center"><%# String.Format("{0:F2}", Eval("[price]")) %></td>
...

Or if you want to use .NET currency formatting:

...
<td  runat="server" id="price" align="center"><%# String.Format("{0:C}", Eval("[price]")) %></td>
...

Check this article for more specific information about .NET Standard Numeric Format String: http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

Henri Hietala
  • 3,021
  • 1
  • 20
  • 27