Createa Custom DataFormatString in a GridView

  • Thread starter Thread starter crjunk
  • Start date Start date
C

crjunk

I have a gridview with a column named TimeOver. Currently, it will
return a value, such as 25.52.

Can the DataFormatString property for the TimeOver data column be
modified so that it display "25 Hours 52 Minutes" instead of just
"25.52"?

I'm trying not to modify the sql function that is computing this value
for me. The reason why is to complex to try and explain in this
message.


Thanks,
C.R. Junk
 
I came across an alternate solution based on the following article:
http://www.netomatix.com/development/gridviewcustomdataformatting.aspx

<asp:GridView ID="ctlGridView" runat="server"
autogeneratecolumns=False
gridlines=Horizontal onrowdatabound="OnRowDataBound">
<columns>
<asp:boundfield datafield="ID" sortexpression="ProductID" />
<asp:boundfield datafield="FirstName" headertext="First Name"
sortexpression="Name" />
<asp:boundfield datafield="LastName" headertext="Last Name" />
<asp:BoundField DataField="DOB" HeaderText="Date Of Birth"
HtmlEncode="False" DataFormatString="{0:d}" />
<asp:BoundField DataField="TimeOver" HeaderText="Time Over"
HtmlEncode="False" />
</columns>
</asp:GridView>


Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs)
'' This protected sub is used to add the Hours and Minutes to
the Time Over values.
'' Otherwise, the user would see 24.50 instead of 24H 50M in
the Time Over grid column.

Dim ea As GridViewRowEventArgs = TryCast(e,
GridViewRowEventArgs)

If ea.Row.RowType = DataControlRowType.DataRow Then

Dim drv As DataRowView = TryCast(ea.Row.DataItem,
DataRowView)
Dim ob As Object = drv("TimeOver")

If ob.ToString <> "STATUS" And ob.ToString <> "DELINQ"
Then

Dim cell As TableCell = ea.Row.Cells(8)
cell.Text = Replace(cell.Text.Trim, ".", "Hours ") &
"Minutes"
End If

End If
End Sub



Thanks,
C.R. Junk
 
Back
Top