You can format grids in 3 ways – via two methods involving data object property attributes and by setting formats directly in grid columns. The example below illustrates usage of formats in an application.

C# Copy imageCopy
//Some data object
public class Product
{
    private DateTime maturity;
    private DateTime startDate;

    [Format("yyyy-MM-dd", "**** ", " #####")]
    public DateTime Maturity
    {
        get { return maturity; }
        set { maturity = value; }
    }

    public DateTime StartDate
    {
        get { return startDate; }
    }
}

public void PopulateGrid(Grid grid)
{
    //Initialize the grid
    grid.Headers.Add(new Header());
    grid.Headers[0].Add(new Column("Maturity"));
    grid.Headers[0].Add(new Column("StartDate"));
    grid.Headers[0]["StartDate"].Format = new StringFormat("yyyy-MM-dd");

    grid.Rows.Add(new Product());

    Console.WriteLine("Maturity: {0}", grid.Rows[0]["Maturity"].Text);
    Console.WriteLine("Start date: {0}", grid.Rows[0]["StartDate"].Text);
}

//Console output:
//Maturity: **** 2009-10-20 #####
//Start date: 2009-10-20

Back to .Net Grid HowTo topics