Solved

formatting numbers in DataGridview (Windows Form) C#

Posted on 2008-06-18
3
4,900 Views
Last Modified: 2008-06-22
Hi,

 

I am showing data in Datagridview and formatting numbers in Datagridview for second column but it is not working.



 

Thanks!!!

Here is my sample code.
 

 
 

    dataGridview1.Columns.Clear();

            dataGridview1.AutoGenerateColumns = false;

            dataGridview1.DataSource = dataTable;
 

            DataGridViewTextBoxColumn amc = new DataGridViewTextBoxColumn();

            amc .DataPropertyName = "AMC";

            amc .Width = 210;

            amc.HeaderText = "";

            amc.ReadOnly = true;

            dataGridview1.Columns.Add(amc);
 

     DataGridViewTextBoxColumn total = new DataGridViewTextBoxColumn();

            total.DataPropertyName = "TotalVal";

            total.Width = 150;

            total.HeaderText = "Total";

            total.ReadOnly = true;

            dataGridview1.Columns.Add(total);
 

            DataGridViewTextBoxColumn perc = new DataGridViewTextBoxColumn();

            perc.DataPropertyName = "TotalYoy";

            perc.Width = 100;

            perc.HeaderText = "Total Perc";

            perc.ReadOnly = true;

            dataGridview1.Columns.Add(perc);
 

     
 

// Format the value 
 

 dataGridview1.Columns[1].DefaultCellStyle.Format = "c";

 dataGridview1.Columns[1].ValueType = typeof(System.Int32);

Open in new window

0
Comment
Question by:r_pat72
  • 2
3 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 21817371
According to msdn, (http://msdn.microsoft.com/en-us/library/f9x2790s.aspx)
the code  
dataGridview1.Columns[1].DefaultCellStyle.Format = "c";
is OK.

I'd try to change value type for the column to e.g. double  - currency is not integer type.
And also swap last two lines:

// Format the value
  dataGridview1.Columns[1].ValueType = typeof(System.Double);
 dataGridview1.Columns[1].DefaultCellStyle.Format = "c";

 
0
 

Author Comment

by:r_pat72
ID: 21817915
Still It is not formatting. It is displaying as 32232.32222
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 21818289
Yeah, I see...

It looks like the problem is in your source  datatable. If the table has string type for a corresponding column (datatable column), your formatting won't work.

Try to set datatable column type to "Decimal" (not to double - my mistake, sorry) before filling it with data
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

20 Experts available now in Live!

Get 1:1 Help Now