Solved

Formatting a negative number in datagrid in asp.net

Posted on 2004-10-06
7
614 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 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

17 Experts available now in Live!

Get 1:1 Help Now