绑定字典的GridView [英] Bind Dictionary to GridView
问题描述
参照此主题:<一href=\"http://stackoverflow.com/questions/9975788/algorithm-to-count-the-time-that-occured-on-the-same-period\">Algorithm计算上同期发生的时候,如何绑定字典到GridView?请看看答案。
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.
我尝试添加一个GridView,然后在code-背后: GV.DataSource = timeRangeCounts
,并将其绑定,但换来的:
I tried to add a GridView and then on the code-behind: GV.DataSource = timeRangeCounts
and bind it, but in return:
ID为GV的GridView的数据源没有任何属性或从属性生成列。确保您的数据源有内容。
我怎样才能做到这一点?请看看下面的code:
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
第二个辅助类是用来帮助字典知道如何一键(类型为 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
第三辅助类存储范围的开始和结束时间:
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()
在GridView:
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
... ...
什么是错的code?
What's wrong with the code?
推荐答案
这里是一个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>
这里是code绑定的字典到的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将执行的ToString
函数,每个值单元。 MyClass的
的
The Gridview will execute the ToString
function of MyClass
, per "Value" cell.
在你的榜样,覆盖在这个类的的ToString
函数
In your example, Override the ToString
function on this class
Public Class TimeRangeCounter
Property ExactRangeMatch as Integer
Property SubRangeMatch as Integer
End Class
这是必要的,因为你的价值是时间 TimeRangeCounter
This is necessary because your "Value" is of time TimeRangeCounter
摘要
作者的code有两个问题。
The Author's code had two problems.
- 问题1产生实际的错误,并按照我的code例如解决
- 2的问题是缺乏在GridView 的值列中使用自定义类一个toString功能
- 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
这篇关于绑定字典的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!