asp.net - 将字典绑定(bind)到 GridView

标签 asp.net vb.net data-binding gridview dictionary

引用此帖子:Algorithm to count the time that occured on the same period 、如何将字典绑定(bind)到GridView上?请看一下答案。

我尝试添加一个 GridView,然后在代码隐藏中:GV.DataSource = timeRangeCounts 并绑定(bind)它,但作为返回:

ID 为“GV”的 GridView 数据源没有任何用于生成列的属性或特性。确保您的数据源有内容。

我怎样才能做到这一点?请看下面的代码:

<小时/>

第一个帮助器类用于保存精确匹配和子范围匹配的计数:

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

    <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>

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

这是输出

enter image description here

如果我的值是某种类型“MyClass”怎么办?

Gridview 将针对每个“Value”单元格执行 MyClassToString 函数。

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

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

这是必要的,因为您的“值”是时间TimeRangeCounter

摘要

作者的代码有两个问题。

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

关于asp.net - 将字典绑定(bind)到 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10377486/

相关文章:

asp.net - 无法从后面的代码更改 CssClass

vb.net - 如何查看在 "with block"中创建的对象?

c# - WPF 动态绑定(bind)

c# - 具有只读属性的数据绑定(bind)

c# - UserManager.Create : An exception of type 'System.MissingMethodException' occurred in mscorlib. dll 但未在用户代码中处理

HTML 参数中的 JavaScript

c# - XmlDocument - 从字符串加载?

c# - 如何将复选框添加到绑定(bind)到数据源的数据 GridView ?

.net - 如果在 TabControl 或 Panel 中查找 Control,Form.Controls 将不返回任何内容

winforms - XtraGrid 在更新其数据源后不刷新