2012-04-30 15 views
3

このスレッドを参照する:Algorithm to count the time that occured on the same period、辞書をGridViewにバインドするにはどうすればいいですか? Plsは答えを見ています。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 

サードヘルパークラスは範囲の開始時刻と終了時刻を格納します。

0123その後、私はそれを実行しようとしたが、結果は似ている

<asp:GridView ID="GV" runat="server"> 
    <Columns> 
     <asp:BoundField DataField="Key" HeaderText="Dictionary Key" /> 
     <asp:BoundField DataField="Value" HeaderText="Dictionary Value" /> 
    </Columns> 
</asp:GridView> 

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:だから私たちは、このアルゴリズムを記述することができるはずの上を使用して

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 

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

グリッドビューのコードを表示してください、ありがとう! –

+0

@Brian Webster:遅く返事を申し訳ありません。私はそれを加えた。 Plsは見てみましょう。結果は得られましたが、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は、MyClassという機能を "値"セルごとに実行します。あなたの例では

、このクラスにToString関数をオーバーライド

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

これはあなたの「価値」は時間であるため

TimeRangeCounter

概要は著者のコードは2を持っていた必要があります問題。問題1は、実際のエラーを生成し、

  • 問題2はGridViewの
  • の「値」列に使用されるカスタムクラスのToString関数の欠如であった私のコード例に従うことによって解決された

    +0

    辞書をGridViewに割り当てようとしましたが、依然としてメッセージが返されます。 Plsはhttp://stackoverflow.com/questions/9975788/algorithm-to-count-the-time-that-occured-on-the-same-period ..を見てくださいありがとう:) –

    +0

    あなたの質問を編集してくださいあなたの正確なコード。あなたのリンクの質問と回答は、私が見なければならないコードに関して少し曖昧です。私が投稿したこのコードは動作します。また、通行人が何が尋ねられているのかを理解するために必要な情報を捜す必要がないように、別の作業単位となるように質問/回答をすることは良いことです。 –

    +0

    上記のコードを編集しました。 Plsは見てみましょう:) –

    関連する問題