473,499 Members | 1,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Formatting a column in a datagridview to currency

I am using this code at the moment to format the column

this.itemsDataGrid.Columns["Amount"].DefaultCellStyle.Format = "c";

but i get the error "Object reference not set to an instance of an
object. " is this code incorrect ?
Any suggestions please
Jun 27 '08 #1
3 10347
On May 9, 8:53 am, Cdude <camelj...@yahoo.comwrote:
I am using this code at the moment to format the column

this.itemsDataGrid.Columns["Amount"].DefaultCellStyle.Format = "c";

but i get the error "Object reference not set to an instance of an
object. " is this code incorrect ?
Any suggestions please
Got it thanks
Jun 27 '08 #2
Well, is there a column named "Amount"?

Note that a *column* named "Amount" is different to a column that is
mapped to the *property* named "Amount"...

Marc
Jun 27 '08 #3
One other thought - if you are letting the grid auto-generate the
columns by attaching a DataSource (rather than by creating explicit
columns), then you need to be aware that the columns won't exist until
the grid needs to display itself. In which case, you can use the
ColumnAdded event as a prompt (below).

Marc

grid.ColumnAdded += (s, a) =>
{
if (a.Column.DataPropertyName == "Amount")
{
a.Column.DefaultCellStyle.Format = "c";
}
};using System;
using System.ComponentModel;
using System.Windows.Forms;

class Foo
{
public decimal Amount { get; set; }
}

class Program
{
[STAThread]
static void Main()
{
BindingList<Foodata = new BindingList<Foo>();
data.Add(new Foo {Amount = 1.2M});
data.Add(new Foo {Amount = 7.8M});

DataGridView grid = new DataGridView();
grid.Dock = DockStyle.Fill;
grid.DataSource = data;

grid.ColumnAdded += (s, a) =>
{
if (a.Column.DataPropertyName == "Amount")
{
a.Column.DefaultCellStyle.Format = "c";
}
};

Application.EnableVisualStyles();
Application.Run(new Form {Controls = {grid}});

}
}
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1855
by: Not Me | last post by:
Hi, I have a table with a currency field, which is displayed via a form. If I open a query which displays this field (using the forms!myform.field notation) that field displays as though it's...
2
5100
by: Lynn N. | last post by:
I have a report showing Rate, Hours and Total Pay (which is Rate*Hours) for several workers. I want to sum the Total Pay and get a CORRECT figure. This seems like it should be such a simple task....
5
7684
by: pelcovits | last post by:
How do I get Access to put a blank in a report column (but not eliminate the record completely), when the field in question is currency format, and the value is zero? My understanding is that null...
1
963
by: Jon Spivey | last post by:
Hi, simple question I'm sure - just moved a site from a UK based host to a US based one. All my currency amounts are now showing as $123.45 instead of £123.45. How do I get them back to UK (£)...
5
4551
by: cwbp17 | last post by:
Hi all, Have a datagrid that displays the price column of a table. Went to the Datagrid's Property builder and set the 'Data Formatting expression' for the PRICE column to {0:c}, so that the...
1
1511
by: Parveen | last post by:
This is a pretty simple issue to solve...but i can't seem to figure it out!! I want data in some columns to show as currency so I set the format of a column to currency through code by ...
2
1952
by: Mitchell Vincent | last post by:
I have a bunch of text boxes and labels that hold currency values. I need some formatting on them, but since they are databound to a set, I'm confused as to how to do that. Do labels and text boxes...
3
11982
by: Mike L | last post by:
Data in datagrid shows all numbers to the 1000th place. So, $3.00 is shown 3.0000 How do I set the datagrid column to currency format? DataGridColumnStyle colStyle4 = new...
9
1645
by: active | last post by:
I need a control that displays a grid of rectangles. That is, like a brick wall except the rectangles are lined up. In VB6 I used such a control (may have been called FlexGrid but I'm not...
2
2063
by: =?Utf-8?B?VG9jbw==?= | last post by:
Hello. My goal is to pain the entire row fore color as Red. If the value of column 24 is true (boolean), I wish to paint the row as Red. Otherwise, leave row as default (no formatting). The...
0
7014
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7229
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
311
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.