Solved

Formatting a negative number in datagrid in asp.net

Posted on 2004-10-06
7
633 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to refer to value 8 81
IEnumerable<T> to a List<T> 8 53
Testing of the web services 1 50
ASP.NET Complete Life Cycle for all the controls 2 22
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 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