views:

1022

answers:

1

hi, i have a datagridview that i have populated programmatically and i was wondering how i can make one of the columns conform to a specified format - "C2".

Can this be done after the datasource has been assigned?

Cheers.

A: 
       var persons = new[] {new {name = "aaa", salary = 40000}, 
                     new  {name = "aaa", salary = 40000}, 
                     new  {name = "aaa", salary = 40000}, 
                     new  {name = "aaa", salary = 40000}};


    GridView1.DataSource = persons;
    GridView1.AutoGenerateColumns = false;

    var NameField = new BoundField();

    NameField.HeaderText = "Name";
    NameField.DataField = "name";
    GridView1.Columns.Add(NameField);

    var SalaryField = new BoundField();
    SalaryField.HeaderText = "Salary";
    SalaryField.DataField = "salary";
    SalaryField.DataFormatString = "{0:c2}";
    SalaryField.HtmlEncode = false;
    GridView1.Columns.Add(SalaryField);


    GridView1.DataBind();
Rob