2012-04-30 117 views
3

參照此線程:Algorithm to count the time that occured on the same period,如何將字典綁定到GridView?請看看答案。將字典綁定到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 
...    ... 

代碼有什麼問題?

+0

請爲您的Gridview顯示代碼,謝謝! –

+0

@布萊恩韋伯斯特:對不起,回覆晚了。我添加了它。請看看。它得到了結果,但Dictionary Value沒有返回任何值,它只返回'TimeRangeCounter'。 –

+0

請參閱我的編輯,請 –

回答

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 

這裏是輸出

enter image description here

如果我的某些類型的「MyClass?」的值如何?

Gridview將根據「值」單元執行MyClassToString函數。

在你的榜樣,重寫ToString功能上該類

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

這是必要的,因爲你的「價值」是時間TimeRangeCounter

摘要

筆者的代碼有兩個問題。

  • 問題1是產生實際誤差,並通過下面的我的代碼示例
  • 問題2解決是缺乏用於在將GridView
「值」列中使用的自定義類一個ToString函數的
+0

我試圖將Dictionary分配給GridView,但它仍然返回消息。請看看http://stackoverflow.com/questions/9975788/algorithm-to-count-the-time-that-occured-on-the-same-period ..謝謝:) –

+0

請編輯您的問題,包括你的確切代碼。鏈接中的問題和答案對於我應該查看的代碼有點模糊。我已發佈的這段代碼有效。此外,對於SO的問題/回答也是很好的,因爲這些路人不必去追查需要了解所要求的信息。 –

+0

我編輯了上面的代碼。請看看:) –