Solved

Formatting a negative number in datagrid in asp.net

Posted on 2004-10-06
7
599 Views
Last Modified: 2008-03-04
Is it possible to format a negative integer in a column in datagrid to show (1) instead of -1.

Ex:
  Column                           Column(This is what I want)
  -1                                            (1)
 -20                                            (20)

Thanks in advance.
0
Comment
Question by:wilkersons
7 Comments
 
LVL 18

Accepted Solution

by:
tusharashah earned 125 total points
ID: 12240247
You can do something like following in your ItemDataBound event of DataGrid:
---------------------------------------------------------------------------------------------------------------------------------------
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
      switch( e.Item.ItemType )
      {
      
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                  string strNumber = e.Item.Cells[2].Text;
                  if( strNumber.IndexOf("-") > -1 )
                                                {
                                                      strNumber = strNumber.Replace("-","");
                                                      strNumber = "(" + strNumber + ")";        
                                                }

                                break;
      }
}
---------------------------------------------------------------------------------------------------------------------------------------                              

-tushar            
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12240271
You just need to replace 2 here with your Column Number

e.Item.Cells[2].Text;
0
 
LVL 10

Assisted Solution

by:jnhorst
jnhorst earned 125 total points
ID: 12240469
Do this in the ItemDataBound event.

If e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Footer Then
     ' replace the (x) with the column index in question.
     ' if you are dealing with integers only, use System.Int32.Parse()
     e.Item.Cells(x).Text =  System.Int32.Parse(e.Item.Cells(x).Text).ToString("#;(#)")

     ' if you are dealing with floats (numbers with decimals), use System.Decimal.Parse()
     e.Item.Cells(x).Text =  System.Decimal.Parse(e.Item.Cells(x).Text).ToString("#.##;(#.##)")
End If

John



0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 10

Expert Comment

by:jnhorst
ID: 12240501
You can also do this:

e.Item.Cells(x).ForeColor = Color.Red

to show the negative values in red.

John
0
 
LVL 33

Expert Comment

by:raterus
ID: 12240580
There is a more elegant way to do this, than the way tusharashah suggests.  I can't say it's any easier though :-)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemglobalizationnumberformatinfoclassnumbernegativepatterntopic.asp
0
 

Author Comment

by:wilkersons
ID: 12241217
Thanks a lot  everyone for ur suggestions.
I tried
Case ListItemType.Item Or ListItemType.AlternatingItem
                             Dim strNumber As String
                strNumber = e.Item.Cells(6).Text
                If (strNumber.IndexOf("-") > -1) Then

                    strNumber = strNumber.Replace("-", "")
                    strNumber = "(" & strNumber & ")"
                    e.Item.Cells(6).Text = strNumber
                End If

AND I ALSO TRIED
Case ListItemType.Item Or ListItemType.AlternatingItem
                e.Item.Cells(6).Text = System.Int32.Parse(e.Item.Cells(6).Text).ToString("#,##;(#,##)")

BOTH OF THEM WORKED.

Thanks a lot

0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12241295
Nice to have you going!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now