Divide number in grid cell

Discussion in 'ASP .Net Datagrid Control' started by Jason ., May 4, 2006.

  1. Jason .

    Jason . Guest

    I thought this would be fairly easy to do but I can't seem to get it
    right. I would like to divide the number displayed in my datagrid cell
    by one million. For example, my data looks like this:

    34,000,000,000 and I would like to display it like 34,000.

    Can I do this from the datagrid?

    *** Sent via Developersdex http://www.developersdex.com ***
     
    Jason ., May 4, 2006
    #1
    1. Advertising

  2. Jason .

    MasterGaurav Guest

    Use templated column.
    None of the available columns would do what you are doing... unless I
    am on the wrong side.


    --
    Cheers,
    Gaurav Vaish
    http://mastergaurav.org
    http://www.edujini.in
    -------------------------------
    To Hell with Murphy's Law.. When things go broke, FIX Them
    -------------------------------
     
    MasterGaurav, May 4, 2006
    #2
    1. Advertising

  3. Jason .

    Jason . Guest

    Thanks. I converted to a Template column but I am unsure of the syntax.

    <%# DataBinder.Eval(Container, "DataItem.BaseValue",
    "{0:###,###,##0.00}") %>

    I need to divide my BaseValue by 100,000 prior to displaying.



    *** Sent via Developersdex http://www.developersdex.com ***
     
    Jason ., May 4, 2006
    #3
  4. Hi Jason,

    I'd use a helper function for this. In the code below, you can see that I
    call BaseValueHelper which does the division and formats the string before
    returning it.

    Let us know if this helps?

    Ken
    Microsoft MVP [ASP.NET]


    <%@ page language="VB" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    Protected Sub Page_Load _
    (ByVal sender As Object, _
    ByVal e As System.EventArgs)
    If Not IsPostBack Then
    dg.DataSource = CreateDataSource()
    dg.DataBind()
    End If
    End Sub

    Function BaseValueHelper(ByVal inVal As Double) As String
    Dim dblTemp As Double
    Try
    dblTemp = inVal / 100000
    Catch exc As Exception
    Return "*Error*"
    End Try
    Return Format(dblTemp, "###,###,###.00")
    End Function

    Function CreateDataSource() As Data.DataTable
    Dim dt As New Data.DataTable
    Dim dr As Data.DataRow
    dt.Columns.Add(New Data.DataColumn _
    ("BaseValue", GetType(Double)))
    dt.Columns.Add(New Data.DataColumn _
    ("StringValue", GetType(String)))
    Dim i As Integer
    For i = 0 To 1
    dr = dt.NewRow()
    dr(0) = i + 34000000000
    dr(1) = "Item " + i.ToString()
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title>Get the value of a column</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:datagrid id="dg" runat="server" autogeneratecolumns="False"
    >

    <columns>
    <asp:templatecolumn headertext="BaseValue">
    <itemtemplate>
    <asp:label runat="server" text='<%#
    BaseValueHelper(DataBinder.Eval(Container, "DataItem.BaseValue"))
    %>'></asp:label>
    </itemtemplate>
    </asp:templatecolumn>
    <asp:boundcolumn datafield="StringValue"
    headertext="StringValue"></asp:boundcolumn>
    </columns>
    </asp:datagrid>
    </div>
    </form>
    </body>
    </html>

    "Jason ." <> wrote in message
    news:O0%...
    > Thanks. I converted to a Template column but I am unsure of the syntax.
    >
    > <%# DataBinder.Eval(Container, "DataItem.BaseValue",
    > "{0:###,###,##0.00}") %>
    >
    > I need to divide my BaseValue by 100,000 prior to displaying.
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
     
    Ken Cox - Microsoft MVP, May 7, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.rcn.com
    Replies:
    1
    Views:
    9,801
    Chris Smith
    Jul 12, 2004
  2. pat270881
    Replies:
    4
    Views:
    490
    Rogan Dawes
    Apr 6, 2005
  3. Rahul
    Replies:
    1
    Views:
    436
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
    Jul 15, 2006
  4. Replies:
    8
    Views:
    478
    osmium
    Oct 14, 2006
  5. utab
    Replies:
    25
    Views:
    955
    terminator
    Nov 23, 2007
Loading...

Share This Page