Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7869539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:21:21+00:00 2026-06-03T01:21:21+00:00

Referring to this thread: Algorithm to count the time that occured on the same

  • 0

Referring to this thread: Algorithm to count the time that occured on the same period, how to bind the dictionary to the GridView? Pls take a look to the answer.

I tried to add a GridView and then on the code-behind: GV.DataSource = timeRangeCounts and bind it, but in return:

The data source for GridView with id 'GV' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.

How can I do that? Please take a look at the code below:


The first helper class is used to hold the counts of exact and sub range matches:

Public Class TimeRangeCounter
    Property ExactRangeMatch as Integer
    Property SubRangeMatch as Integer
End Class

The second helper class is used to help the dictionary know how one key (of type TimeRange) differs from another:

Public Class TimeRangeEqualityComparer 
    Implements IEqualityComparer(Of TimeRange)

    Public Overloads Function Equals(left As TimeRange, right As TimeRange) _
            As Boolean Implements IEqualityComparer(Of TimeRange).Equals           

        Return left.ToString = right.ToString   
    End Function

    Public Overloads Function GetHashCode(range As TimeRange) _
            As Integer Implements IEqualityComparer(Of TimeRange).GetHashCode

        return range.ToString().GetHashCode()
    End Function

End Class

The Third helper class stores the start and end times of a range:

Public Class TimeRange 
    Private readonly _start
    Private readonly _end

    Public Readonly Property Start 
        Get
           return _start
        End Get
    End Property

    Public Readonly Property [End] 
        Get
           return _end
        End Get
    End Property

    Public Sub New(start As String, [end] As string)
        Me._start = start
        Me._end = [end]
    End Sub

    Public Overrides Function ToString() as String
       Return String.Format("{0}-{1}", Start, [End])
    End Function

End Class

So using the above we should be able to write this algorithm:

Dim columnLength As Integer = 5
Dim timeStart() As String = {"08.00", "08.00", "10.00", "08.00", "08.00"}
Dim timeEnd() As String = {"08.50", "11.50", "11.00", "09.00", "08.50"}
Dim comparer As New TimeRangeEqualityComparer()
Dim timeRangeCounts As New Dictionary(Of TimeRange, TimeRangeCounter)(comparer)

'Count exact range matches while building dictionary
For i = 0 to columnLength - 1
  Dim key As TimeRange = New TimeRange(timeStart(i), timeEnd(i))

  If timeRangeCounts.ContainsKey(key)
      timeRangeCounts(key).ExactRangeMatch += 1
  Else
      Dim counter =  New TimeRangeCounter()
      counter.ExactRangeMatch = 1
      timeRangeCounts(key) = counter
  End If        

Next           

'Count sub ranges          
For Each kvp in timeRangeCounts
    For Each key in timeRangeCounts.Keys
        If kvp.key.Start >= key.Start AndAlso _ 
           kvp.Key.End <= key.End AndAlso _
           kvp.key.ToString <> key.ToString then           

            kvp.Value.SubRangeMatch += 1
        End If
    Next
Next

'Console.WriteLine(timeRangeCounts)
    GV.DataSource = timeRangeCounts
    GV.DataBind()

The gridview:

<asp:GridView ID="GV" runat="server">
    <Columns>
        <asp:BoundField DataField="Key" HeaderText="Dictionary Key" />
        <asp:BoundField DataField="Value" HeaderText="Dictionary Value" />
    </Columns>
</asp:GridView>

Then I tried to run it but the result is like:

Dictionary Key    Dictionary Value
08:00:00-08:50:00 TimeRangeCounter
08:00:00-09:40:00 TimeRangeCounter
10:00:00-11:40:00 TimeRangeCounter
...               ...

What’s wrong with the code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:21:22+00:00Added an answer on June 3, 2026 at 1:21 am

    Here is a Gridview

        <asp:GridView ID="GV" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:BoundField DataField="Key" HeaderText="Dictionary Key" />
                <asp:BoundField DataField="Value" HeaderText="Dictionary Value" />
            </Columns>
        </asp:GridView>
    

    Here is code to bind a dictionary to that Gridview

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim D As New Dictionary(Of Integer, String)
        D.Add(1, "One")
        D.Add(2, "Two")
        D.Add(3, "Three")
        GV.DataSource = D
        GV.DataBind()
    End Sub
    

    Here is the output

    enter image description here

    What if my Value of of some type “MyClass?”

    The Gridview will execute the ToString function of MyClass, per “Value” cell.

    In your example, Override the ToString function on this class

    Public Class TimeRangeCounter
        Property ExactRangeMatch as Integer
        Property SubRangeMatch as Integer
    End Class
    

    This is necessary because your “Value” is of time TimeRangeCounter

    Summary

    The Author’s code had two problems.

    • Problem 1 was generating an actual error and was solved by following my code example
    • Problem 2 was the lack of a ToString function for the custom class used in the “Value” column of the Gridview
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm referring this address for function olLiTree PHP function that creates a nested ul
From this article http://www.stuartellis.eu/articles/erb referring to thread safety levels: At this level, the specified
Referring to this question , how can i get the current page size in
referring to this question , I've decided to duplicate the tables every year, creating
Referring to this question: https://stackoverflow.com/questions/2035449/why-is-oop-hard-for-me class Form { protected $inputs = array(); public function
Referring to this web site http://www.cplusplus.com/reference/std/utility/make_pair/ The std::make_pair has this signature (and possible implementation):
Referring to this question, no solution/answer was given for Oracle platform. Selecting Nth Record
I am referring to this project by Jimmy Bogard: http://www.codeplex.com/AutoMapper The code repository site
I'm referring to this Nimbus reference . I tried to set global Font to
I am referring to this question type Churchlist t u = (t->u->u)->u->u In lambda

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.