Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting the data type for a programmatically-created BoundColumn

Status
Not open for further replies.

jasonsalas

IS-IT--Management
Jun 20, 2001
480
GU
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!
 
you could try this
Code:
col = new BoundColumn();
col.HeaderText = "Votes";
col.DataField = Format("Votes", "###,##0");
dg.Columns.Add(col);
This will format the column and if no value is present a 0 will be displayed. I tried doing the same thing with a dollar amound bound column. I ended up doing col.DataField = Format ([myField], "currency").

Jason Meckley
Database Analyst
WITF
 
Nice tip, but I now get an error message on that line reading:


"CS0103: The name 'Format' does not exist in the class or namespace 'ASP.<MYUSERCONTROLNAME>'"
 
That didn't take either...it just writes out the lietral text "###,##0" :(
 
Actually, now that I read your post (not just jmeckly's :)), you may want to either utilize the ItemDataBound event to do some casting or read the XML into a strongly typed DataTable via the DataSet.ReadXml() method then bind that DataTable to the grid.
 
Funny you mention the ReadXml() method...that's what I'm using. Oddly, this works perfectly whern reading straight out of the database, but not when reading from an XML file.
 
actually, this didn't work either:

"{0:#,##0}"

I worked around the problem by rewriting the app a bit...and lo and hehold it worked out better than before.
 
{0:#,##0}"

You're cursed! It worked for me.

"lo and hehold it worked out better than before."

Go figure. Aw well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top