Bind Dictionary to GridView

前端 未结 1 853
别那么骄傲
别那么骄傲 2021-01-20 06:57

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 t

相关标签:
1条回答
  • 2021-01-20 07:33

    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

    enter image description here

    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
    0 讨论(0)
提交回复
热议问题