Divide number in grid cell

J

Jason .

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?
 
M

MasterGaurav

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

Jason .

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.
 
K

Ken Cox - Microsoft MVP

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">
<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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top