views:

65

answers:

2

I have come accross a strange problem that can be illustrated using the 2 seperate code blocks below.

If i use the first block, you can clearly see that column 5 has a currency format applied to it.

Using the second block where the only difference is that the string array is added to a datatable and then used as the datasource -> no formatting is applied!

Can anyone explain this behaivour and provide a workaround for me? I am using a large datatable that i will need to format.

Thanks!

Block 1:

string[] list = new string[10];

for (int i = 0; i < 10; i++)
{
    list[i] = i.ToString();
}

this.dataGridViewX1.DataSource = list;
this.dataGridViewX1[0, 5].Style.Format = "C2";

Block 2:

string[] list = new string[10];

for (int i = 0; i < 10; i++)
{
    list[i] = i.ToString();
}

DataTable dt = new DataTable();
dt.Columns.Add();

for (int i = 0; i < list.Length; i++)
{
    DataRow dr = dt.NewRow();
    dr[0] = list[i];

    dt.Rows.Add(dr);
}

this.dataGridViewX1.DataSource = dt;
this.dataGridViewX1[0, 5].Style.Format = "C2";
A: 

the First One uses the datatables binding. use reflector to figure out what Happens there.

Johannes Rudolph
A: 

Don't you need to set the DataColumn DataType?

DataColumn col1;
col1.DataType = Type.GetType("System.DateTime"); // or other type
David Liddle
it was all strings?
Johannes Rudolph