将字典绑定到GridView

3
提到这个主题:Algorithm to count the time that occured on the same period,如何将字典绑定到 GridView?请查看答案。
我尝试添加一个 GridView 然后在代码后端中使用 GV.DataSource = timeRangeCounts 并绑定它,但是返回的结果为: 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. 我该怎么做?请查看下面的代码:
第一个帮助类用于保存准确和子范围匹配的计数:
Public Class TimeRangeCounter
    Property ExactRangeMatch as Integer
    Property SubRangeMatch as Integer
End Class

第二个辅助类用于帮助字典了解一个键(类型为TimeRange)与另一个键的不同之处:
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

第三个辅助类存储范围的开始和结束时间:
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

因此,我们可以使用上述方法编写以下算法:
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()

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>

然后我尝试运行它,但结果是这样的:
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
...               ...

代码有什么问题?

请展示一下您Gridview的代码,谢谢! - Brian Webster
@Brian Webster:抱歉回复晚了。我已经添加了它,请看一下。它得到了结果,但是字典值没有返回任何值,只返回了“TimeRangeCounter”。 - mrjimoy_05
添加了 ToString() 的重写有帮助吗? - Brian Webster
1个回答

5

这是一个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>

这里是将字典绑定到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

这是输出结果

输入图像描述

如果我的某个类型的值是"MyClass?"怎么办?

Gridview将对"Value"单元格执行MyClassToString函数。

在您的示例中,重写此类的ToString函数。

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

这是必要的,因为您的“Value”是时间类型 TimeRangeCounter

摘要

作者的代码存在两个问题。

  • 问题1导致实际错误,通过遵循我的代码示例解决了该问题
  • 问题2是Gridview中“Value”列使用自定义类时缺少ToString函数

我尝试将Dictionary分配给GridView,但它仍然返回消息。请查看http://stackoverflow.com/questions/9975788/algorithm-to-count-the-time-that-occured-on-the-same-period.. 谢谢 :) - mrjimoy_05
请编辑您的问题,包括您的确切代码。您链接中的问题和答案在涉及我应该查看哪个代码方面有些模糊。我发布的这段代码是有效的。此外,在SO上提出的问题/答案最好是独立的工作单元,以便路人不必寻找必要的信息才能理解所提出的问题。 - Brian Webster

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接