Solved

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

Posted on 2006-06-27
8
458 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
 

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
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).

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

11 Experts available now in Live!

Get 1:1 Help Now