locked
Need help in GridView RRS feed

  • Question

  • User-184135847 posted

    <asp:GridView ID="Gridview1" runat="server"  />
                  <Columns>
                    <asp:BoundField DataField="Years" HeaderText="Year" />
                    <asp:BoundField DataField="Hours" HeaderText="Hours" />
                  </Columns>
    </asp:GridView>

    I have above Gridview in my aspx page. but I need to format my Hours column from hour to (Day, hour) format
    for e.g. for 72 hours it should display as 9 d 0 hr.

    For that I have one function in my .vb page as follow,

    Public Function ConvertHoursToDays(ByVal totalHours As Integer) As Object
            Dim num As Integer = CInt(Math.Round(Conversion.Int(CDbl((CDbl(totalHours) / 8)))))
            If (num < 0) Then
                num += 1
            End If
            Dim num2 As Integer = (totalHours Mod 8)
            Return (Conversions.ToString(num) & " d " & Conversions.ToString(num2) & " hr ")
        End Function


    Can I use the function to display gird data in (0 d 0 hr) format ?

    I really appreciate your help.

    Monday, August 24, 2009 3:30 PM

Answers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2009 7:46 AM
  • User244619866 posted

    unfortunatelly there is no format type available to format hours to day and hours. The function you have described, is also can't be use at aspx page at binding time of GridView. There are two approch to get your task done

    1. In first method, override the row databound  event of GridView, get value of hours from databinder, find the custom template field item, use your function to get hours in day & hours and set values to custom template field item.

    2. In second approch, Add a new column in dataset, read the dataset one by one and then set new field value by calling your function.  Bind hours column of datagrid to this new column name.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 29, 2009 1:11 AM

All replies

  • User277595955 posted

    Have you considered using the TimeSpan structure instead?  That might be a cleaner approach.

    I hope this helps you.

    Monday, August 24, 2009 3:57 PM
  • User1282917655 posted

    <asp:GridView ID="Gridview1" runat="server"  />
                  <Columns>
                    <asp:BoundField DataField="Years" HeaderText="Year" />
                    <asp:BoundField DataField="Hours" HeaderText="Hours" />
                  </Columns>
    </asp:GridView>

    I have above Gridview in my aspx page. but I need to format my Hours column from hour to (Day, hour) format
    for e.g. for 72 hours it should display as 9 d 0 hr.

    For that I have one function in my .vb page as follow,

    Public Function ConvertHoursToDays(ByVal totalHours As Integer) As Object
            Dim num As Integer = CInt(Math.Round(Conversion.Int(CDbl((CDbl(totalHours) / 8)))))
            If (num < 0) Then
                num += 1
            End If
            Dim num2 As Integer = (totalHours Mod 8)
            Return (Conversions.ToString(num) & " d " & Conversions.ToString(num2) & " hr ")
        End Function


    Can I use the function to display gird data in (0 d 0 hr) format ?

    I really appreciate your help.

    you can add a templatefield and call that function there

    <ItemTemplate>
    
    <%# someFunction(Eval("SomeVal")) %>
    
    </ItemTemplate>

    keep in mind that function should be public or protected.

    Friday, August 28, 2009 6:37 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2009 7:46 AM
  • User244619866 posted

    unfortunatelly there is no format type available to format hours to day and hours. The function you have described, is also can't be use at aspx page at binding time of GridView. There are two approch to get your task done

    1. In first method, override the row databound  event of GridView, get value of hours from databinder, find the custom template field item, use your function to get hours in day & hours and set values to custom template field item.

    2. In second approch, Add a new column in dataset, read the dataset one by one and then set new field value by calling your function.  Bind hours column of datagrid to this new column name.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 29, 2009 1:11 AM