Solved

Formatting a datagrid column with number of decimal places at runtime?

Posted on 2006-06-27
8
461 Views
Last Modified: 2011-04-14
Hi there,

Does anybody know if its possible to reformat a column with number of decimal places at runtime....

Basically the database returns something like this 45672321 but actually it is really 456723.21  i.e. 2 decimal places...

is it possible to get the datagrid to reformat this information in column x for example ?

Thanks in advance

Ian
0
Comment
Question by:ianinspain
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:sumix
ID: 16996209
Can't you just divide all the values by 100?
0
 

Author Comment

by:ianinspain
ID: 16999455
Yes .... but how would i do this.. my data is in the dataset already..

So what do i need to do, how do i make all values divide by 100....

Do i need to do this on grid...

A little lost here, that is what i need to do is divide all values by 100 but i get the data from a stored procedure that i don't have control of..... so the data arrives like that... so i do need to format in my prog/datagrid

Thanks in advance

Ian
0
 
LVL 12

Expert Comment

by:sumix
ID: 16999516

You could loop the rows in the table and modify each value, something like
foreach (DataRow row in dataTable1.Rows)
{
row["number"] = Convert.ToDecimal(row["number"]) / 100;     // "number" is ColumnName
}

beware if the column type is Integer (if it is dynamicaly created when you call the sp) you will need to change it to decimal
dataTable1.Columns["number"].DataType  = typeof(decimal);

Another option is to add a new column to the table, set its Expression property to "number/100", and show this column in the datagrid (this will be readonly, use it if you dont need to modify this column from the datagrid)

dataTable1.Columns.Add("Newcol").Expression = "number/100";
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:ianinspain
ID: 16999549
Thanks sumix, this is exactly what i need... using your example

dataTable1.Columns.Add("Newcol").Expression = "number/100";

What if the column already exists, can i not update the column to include the expression?

The column i am unsure what type it is... I guess its a string :-) ...

It was an XML file that i had (which i get by calling a webservice which in turn calls a Stored procedure) which i used readxml on a dataset object to convert the xml to a dataset..

I don't think there is anyway to control how the XML is converted i.e. to use different types of fields i.e. Integer, decimal, etc

I think looping the rows would also work but this is more work for the pc i should imagine ?

Thanks for your continued support

Ian
0
 
LVL 12

Accepted Solution

by:
sumix earned 500 total points
ID: 16999662
Of course, if the column exists you can just set its Expression property. If you're not sure about the type of the column return by the web service you can create the expression like

            dataTable1.Columns.["Newcol"].Expression = "Convert(number,'System.Decimal')/100";

The other approach is a litle more expensive indeed, depends of how many rows you expect, and if you need to modify the values.

Another note, if you know the structure of the data returned by the webservice (and this is stable), you could create a typed DataSet where you can control column types and load here the data.

Regards,
sumix

0
 

Author Comment

by:ianinspain
ID: 17000505
Great! I must look into creating typed dataset... thanks for all your help... I am assigning points now!

Actually I am almost complete with all my issues with datagrid... learnt quite a lot today.... but i am missing 1 thing... Doing a subtotal/total at the bottom of the grid.... I actually have a field being returned with the data in there, so i don't need to do any calculations etc...

I just need to leave a white line (space) ... and then print total...

Here is the question if you think you can help .... thanks .. http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21900702.html
0
 
LVL 12

Expert Comment

by:sumix
ID: 17000664
A simple solution, that is why I post it here: how about adding a new empty line in the table before adding the Total line:
    dataTable1.Rows.Add(dataTable1.NewRow());
0
 

Author Comment

by:ianinspain
ID: 17007838
cool! Excellent ... that worked.. thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

830 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