将字典绑定到GridView [英] Bind Dictionary to GridView
问题描述
参考此线程:算法计算同一时期发生的时间,如何将字典绑定到GridView?请仔细看看答案。
我试图添加一个GridView,然后在代码隐藏中: GV.DataSource = timeRangeCounts
并绑定它,但作为回报:
具有id'GV'的GridView的数据源没有任何属性或生成列的属性。确保您的数据源有内容。
我该怎么做?请看下面的代码:
第一个帮助类用于保存精确和子范围的计数匹配:
公共类TimeRangeCounter
属性ExactRangeMatch为整数
属性SubRangeMatch为整数
结束类
第二个帮助类用于帮助字典知道一个键(类型 TimeRange
)与另一个不同:
公共类TimeRangeEqualityComparer
实现IEqualityComparer Of TimeRange)
公共重载函数等于(左As TimeRange,Right As TimeRange)_
As Boolean实现IEqualityComparer(Of TimeRange).Equals
返回左边。 ToString = right.ToString
结束函数
公共重载函数GetHashCode(范围作为TimeRange)_
As Integer实现IEqualityComparer(Of TimeRange)。 GetHashCode
返回范围.ToString()。GetHashCode()
结束函数
结束类
第三帮助类存储范围的开始和结束时间:
Public Class TimeRange
私人readonly _start
私人只读_end
公开只读属性开始
获取
返回_start
结束获取
结束属性
公共只读属性[结束]
获取
返回_end
结束获取
结束属性
公开Sub New(start As String,[end] As string)
Me._start = start
Me._end = [end]
End Sub
公共覆盖函数ToString()as String
返回String.Format({0} - {1},开始,[结束])
结束函数
结束类
所以使用上面我们应该可以写这个算法: p>
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作为新词典(TimeRange,TimeRangeCounter)(比较器)
'在构建字典时计算精确范围匹配
对于i = 0到columnLength - 1
Dim键As TimeRange = New TimeRange(timeStart(i),timeEnd(i))
如果timeRangeCounts.ContainsKey(key)
timeRangeCounts(key).ExactRangeMatch + = 1
Else
Dim counter = New TimeRangeCounter()
counter.ExactRangeMatch = 1
timeRangeCounts(key)= counter
End If
下一个
'计数子范围
对于timeRangeCounts中的每个kvp
TimeRangeCounts.Keys中的每个键
如果kvp.key.Start> = key.Start AndAlso _
kvp.Key.End< = key.End AndAlso _
kvp.key.ToString<> key.ToString然后
kvp.Value.SubRangeMatch + = 1
结束如果
下一个
下一个
'Console.WriteLine(timeRangeCounts )
GV.DataSource = timeRangeCounts
GV.DataBind()
:
< asp:GridView ID =GVrunat =server>
<列>
< asp:BoundField DataField =KeyHeaderText =Dictionary Key/>
< asp:BoundField DataField =ValueHeaderText =Dictionary Value/>
< / Columns>
< / asp:GridView>
然后我尝试运行它,但结果如下:
词典关键字典价值
08:00:00-08:50:00 TimeRangeCounter
08:00:00-09:40:00 TimeRangeCounter
10:00:00-11:40:00 TimeRangeCounter
... ...
代码有什么问题?
这是一个Gridview p>
< asp:GridView ID =GVrunat =serverAutoGenerateColumns =false>
<列>
< asp:BoundField DataField =KeyHeaderText =Dictionary Key/>
< asp:BoundField DataField =ValueHeaderText =Dictionary Value/>
< / Columns>
< / asp:GridView>
这里是将字典绑定到该Gridview的代码
受保护的子Page_Load(ByVal sender As Object,ByVal e As System.EventArgs)处理Me.Load
Dim D作为新字典(的整数,字符串)
D.Add(1,One)
D.Add(2,Two)
D.Add(3,Three)
GV.DataSource = D
GV.DataBind()
End Sub
输出
如果我的某些类型的MyClass的价值
Gridview将按Value单元格执行 MyClass
的 ToString
函数。
在您的示例中,覆盖此类的 ToString
函数
公共类TimeRangeCounter
/ pre>
属性ExactRangeMatch as Integer
属性SubRangeMatch as Integer
结束类
这是必要的,因为您的值是时间
TimeRangeCounter
摘要
作者的代码有两个问题。
- 问题1产生了一个实际的错误,并通过遵循我的代码示例来解决。
- 问题2是缺少用于自定义类中的自定义类的ToString函数Gridview的Value列
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?
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
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
这篇关于将字典绑定到GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!