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
Note that a *column* named "Amount" is different to a column that is
mapped to the *property* named "Amount"...
Marc
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<Foo> data = 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}});
}
}