Format output of dynamic Datagrid columns

  • Thread starter Thread starter J.B
  • Start date Start date
J

J.B

I have a datagrid that will display different datasets, (it runs
different sprocs based on a value in the querystring) but it will
always return 6 columns. The 5th and 6th columns are always Date and
Currency format, all others are strings.

I'd like to build one datagrid to handle this datagrid dynamically. I
have it working now, all except the formatting of the 5th (Date) and
6th (Currency) columns.

Since the headers for those columns will be different depending on the
situation (and the sprocs that is kicked off) I need to format these
columns on the fly.

I have this as my DataGrid:


<ASP:Datagrid id="dgAssetInfo"
runat="server"
AutoGenerateColumns="True"
CellPadding="5"
Font-name="Arial"
Font-size="11px"
OnItemDataBound="dgAssetInfo_ItemDataBound"<AlternatingItemStyle BackColor = "#DDDDCC" />
<HeaderStyle CssClass="update-header" ForeColor="White" />
<ItemStyle CssClass="text" />
</asp:datagrid>

I am trying to format the Columns using ItemDataBound, basically
saying "make the 5th column look like a date" ({0:D} format).

I experimented with this, but no luck...

Sub dgAssetInfo_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)

'Format 5th column as Date
E.Item.Cells(4).DataFormatString = {0:D}

'Format 6th column as Currency
E.Item.Cells(5).DataFormatString = {0:C}

End Sub

Can I do this in ItemDataBound? I've looked at the Properties of the
TableCell class but nothing looks like it will allow formatting of
each cell.

Any ideas on how to do this?? Should I be doing it differently (and
no, unfortunately, I can't modify the sprocs to format the data prior
to returning the data)
 
Just an idea,

Why not letting your database formating the output data to date and
Currency format.

Natty Gur[MVP]
Phone Numbers:
Office: +972-(0)9-7740261
Fax: +972-(0)9-7740261
Mobile: +972-(0)58-888377
 
Back
Top