Createa Custom DataFormatString in a GridView

Discussion in 'ASP .Net' started by crjunk, Jan 29, 2009.

  1. crjunk

    crjunk Guest

    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
    crjunk, Jan 29, 2009
    #1
    1. Advertising

  2. crjunk

    crjunk Guest

    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
    crjunk, Jan 29, 2009
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerhard Pretorius

    August CTP DataFormatString in Gridview not working

    Gerhard Pretorius, Sep 6, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,692
    Louis
    Sep 22, 2005
  2. Gerhard Pretorius

    DataformatString in Gridview and Detalsview ignored

    Gerhard Pretorius, Sep 8, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    618
    Gerhard Pretorius
    Sep 8, 2005
  3. Jon

    Gridview and dataformatstring

    Jon, Nov 8, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,584
    =?Utf-8?B?Q3VydCBLb3BwYW5n?=
    Nov 9, 2005
  4. Hardy Wang
    Replies:
    1
    Views:
    5,593
    Hardy Wang
    Dec 12, 2005
  5. Tina
    Replies:
    2
    Views:
    1,608
    lildakes
    Mar 26, 2008
Loading...

Share This Page