Solved

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

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

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

Title # Comments Views Activity
MVC5 Dataformat String for Phone not working 3 25
C# application error "Parameter Is Not Valid". 3 79
In WPF / C# binding a local database in code behind 1 24
SQL Login 17 40
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

895 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