Classic ASP Table to DataGrid...formatting color and decimal places

N

Next

Hello,

I trying to convert an ASP page to ASP.NET using a DataGrid control.

All of the data is being displayed in the DataGrid. But I need help with
formatting.

The asp code segment looks like this:
<%
' list out the metals
do while not rsMetals.eof

intChange = rsMetals(2) - rsMetals(1)

if intChange > 0 or intChange = 0 then
intChange = FormatNumber(intChange, rsMetals(4),,True)
Response.Write "<TR><TD>" & rsMetals(0) & "</TD>" & _
"<TD></TD>" & _
"<TD></TD>" & _
"<TD>$" & FormatNumber(rsMetals(2), rsMetals(4)) & "</TD>" & _
"<TD>$" & intChange & "</TD>" & _
"<TD>" & rsMetals(3) & "</TD></TR>"
else
intChange = FormatNumber(intChange, rsMetals(4),,True)
Response.Write "<TR><TD>" & rsMetals(0) & "</TD>" & _
"<TD></TD>" & _
"<TD></TD>" & _
"<TD>$" & FormatNumber(rsMetals(2), rsMetals(4)) & "</TD>" & _
'-----Decimal places
"<TD><FONT COLOR='#ff0000'>$" & intChange & "</FONT></TD>" & _
'-----Red if < 0
"<TD>" & rsMetals(3) & "</TD></TR>"
end if
rsMetals.MoveNext
loop
rsMetals.close
%>

My DataGrid looks like this:
<asp:DataGrid id="grdPMetals" runat="server" AutoGenerateColumns="False"
Width="100%" CssClass="Content">
<Columns>
<asp:BoundColumn HeaderText="SPOT" DataField="descr"
ItemStyle-Width="34%" />
<asp:BoundColumn HeaderText="Current" DataField="spot"
ItemStyle-Width="13%" />
<asp:BoundColumn HeaderText="Change From<br>Previous Close*"
DataField="change" ItemStyle-Width="19%" />
<asp:BoundColumn HeaderText="Last Update"
DataField="last_mod_date" ItemStyle-Width="34%" />
</Columns>
</asp:DataGrid>

1) If intChange is less than zero, I want to change the color to Red and
show in parenthises instead of a negative sign?

2) My second problem is is formatting the number of decimal places. The
above code uses the function "FormatNumber(intChange, rsMetals(4),,True)"
where rsMetals is the number of decimal places to display.

Any help would be greatly appreciated! Thanks in advance for your time.
Aaron
 
M

Mike Moore [MSFT]

Hi Aaron,

The custom numeric formatting string is your primary option for this.

For info on what you can put inside this format field:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconcustomnumericformatstrings.asp

Also, here is a code sample that demonstrates one way of implementing the
formatting string. Please let me know if this answers your question.

This won't show negative values because the Titles table doesn't contain
negative values. If it did, they would show as red.

* Place a datagrid on your form and then put this in your code-behind.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Bind()
End If
End Sub

Private Sub Bind()
Dim Qry1 As System.Data.SqlClient.SqlDataReader
Dim connectionString As String = "server='localhost';
trusted_connection=true; Database='pubs'"
Dim sqlConnection As System.Data.SqlClient.SqlConnection = New
System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT ISNULL(price, 0) AS Price FROM
titles"
Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)
sqlConnection.Open()
Qry1 =
sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
DataGrid1.DataSource = Qry1
DataGrid1.DataBind()
Qry1.Close()
sqlCommand.Dispose()
sqlConnection.Close()
sqlConnection.Dispose()
End Sub

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
If e.Item.DataItem(0) < 0 Then
CType(e.Item.Cells(0),
System.Web.UI.WebControls.TableCell).ForeColor = System.Drawing.Color.Red
End If
e.Item.Cells(0).Text =
CDbl(e.Item.DataItem(0)).ToString("$#,##0.00;($#,##0.00);N/A")
End If
End Sub


Thank you, Mike Moore
Microsoft, ASP.NET

This posting is provided "AS IS", with no warranties, and confers no rights.


--------------------
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top