Solved

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

Posted on 2006-06-27
8
462 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
[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
  • 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
Technology Partners: 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!

 

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

Independent Software Vendors: 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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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