Solved

formatting numbers in DataGridview (Windows Form) C#

Posted on 2008-06-18
3
5,063 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
[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
  • 2
3 Comments
 
LVL 30

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 30

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Suggested Courses

628 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