DataGrid Column - DataTextFormatString

M

McNutt Consulting

I'm trying to implement a datagrid with dynamically created columns. The
data is coming out of a very simple XML file, and it's bound through a
dataview.

The datagrid is just a shell definition with no column definitions in the
..aspx file.

The grid is binding fine with all the data being displayed, but my numeric
columns aren't being formatted. I'm using the "dataformattextstring", but
it doesn't seem to be working. Is there anything else I need to do?

Thanks for any help you can give,
Larry

example of my code:

Dim dgc as datagridcolumn

dgc.HeaderText = "Title"
dgc.DataNavigateUrlField = "account"
dgc.DataNavigateUrlFormatString = "detail.aspx={0}"
dgc.DataTextField = "account"
dgc.DataTextFormatString = "{0:n}"

dg.Columns.Add(dgc)
 
A

Alvin Bruney

where are you trying to do the formatting? From the contents of your post,
it seems that you want to format items in the autogenerated columns. If
that's the case, what you have used so far applies formatting to the
template columns. Autogenerated columns behave differently because they
aren't added to the column collection so you don't have access to them prior
to databinding. To fix this, add this code to your itemdatabound event
handler

if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)

{



double input = e.Item.Cells[3].Text;

e.Item.Cells[3].Text = input.ToString("#,###,.0");

[snip]

roughly
 
T

Teemu Keiski

Hi,

the string formatting does not work if the underlying type (the type to be
formatted) is string itself as string formatting works from other types to
String. You would need to manage this say in ItemDataBound event, i.e catch
the string value, cast to the correct type and then apply formatting. Might
seem bit overkill, though. So how are you getting the XML? By loading into
DataSet? If yes, you might consider specifying the data type for the
generated DataTable's columns so that you get typed reference to the data
wehn binding (and again formatting to work)
 
M

McNutt Consulting

Thanks,

Your explanation makes sense to me. The XML file doesn't articulate the
type of data (which is numeric). The XML file is generated on the fly from
a COM object and is simply loaded into a dataview via the following
statements:

Dim xtr As XmlTextReader = New XmlTextReader(oXml, XmlNodeType.Document,
Nothing)
ds.ReadXml(xtr, XmlReadMode.Auto)

[oXml is a string variable.]

I will try to set the datatype on the DS or possily use the databind event.
Did want to much overkill like you said. If you could give me a little more
detail about ("consider specifying the data type for the generated
DataTable's columns") I would much appreciate it. Do you mean establish all
the columns of the DS manually?

Thanks for your response.

Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top