jasonsalas
IS-IT--Management
I'm running into problems using BoundColumns in a DataGrid I'm creating programmatically. I'm reading-in values from an XML file as the datasource, and then applying a DataView to filter out specific rows, and apply sorting.
Or so I thought. Here's my code:
DataView dv = new DataView();
dv.Table = aDataSet.Tables[0];
dv.RowFilter = "City = '" + someValue + "'";
dv.Sort = "Votes DESC";
BoundColumn col;
DataGrid dg = new DataGrid();
dg.AutoGenerateColumns = false;
col = new BoundColumn();
col.HeaderText = "Name";
col.DataField = "Name";
col.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
dg.Columns.Add(col);
col = new BoundColumn();
col.HeaderText = "Party";
col.DataField = "Party";
dg.Columns.Add(col);
col = new BoundColumn();
col.HeaderText = "Votes";
col.DataField = "Votes";
col.DataFormatString = "{0:N0}"; // this is what won't come out as I expected
dg.Columns.Add(col);
dg.DataSource = dv;
dg.DataBind();
The line above - col.DataFormatString = "{0:N0}"; - is where my problem lies. I've been able to deduce that the XML data is being seen as a default type of String, so the numerical formatting isn't taking (adding in the comma at the thousand marker), and it's also screwing up my sorting, not being truly numeric.
So, my question is how can I set the data type for a BoundColumn when it's created programtically?
Thanks!
Or so I thought. Here's my code:
DataView dv = new DataView();
dv.Table = aDataSet.Tables[0];
dv.RowFilter = "City = '" + someValue + "'";
dv.Sort = "Votes DESC";
BoundColumn col;
DataGrid dg = new DataGrid();
dg.AutoGenerateColumns = false;
col = new BoundColumn();
col.HeaderText = "Name";
col.DataField = "Name";
col.ItemStyle.HorizontalAlign = HorizontalAlign.Left;
dg.Columns.Add(col);
col = new BoundColumn();
col.HeaderText = "Party";
col.DataField = "Party";
dg.Columns.Add(col);
col = new BoundColumn();
col.HeaderText = "Votes";
col.DataField = "Votes";
col.DataFormatString = "{0:N0}"; // this is what won't come out as I expected
dg.Columns.Add(col);
dg.DataSource = dv;
dg.DataBind();
The line above - col.DataFormatString = "{0:N0}"; - is where my problem lies. I've been able to deduce that the XML data is being seen as a default type of String, so the numerical formatting isn't taking (adding in the comma at the thousand marker), and it's also screwing up my sorting, not being truly numeric.
So, my question is how can I set the data type for a BoundColumn when it's created programtically?
Thanks!