Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 470
  • Last Modified:

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

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
ianinspain
Asked:
ianinspain
  • 4
  • 4
1 Solution
 
sumixCommented:
Can't you just divide all the values by 100?
0
 
ianinspainAuthor Commented:
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
 
sumixCommented:

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ianinspainAuthor Commented:
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
 
sumixCommented:
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
 
ianinspainAuthor Commented:
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
 
sumixCommented:
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
 
ianinspainAuthor Commented:
cool! Excellent ... that worked.. thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now