C# 如何在GridView中动态绑定控件

C# 如何在GridView中动态绑定控件,c#,asp.net,gridview,C#,Asp.net,Gridview,我使用asp.net 4 c# 我有一个gridview,在标签内。我想知道如何将第一个标签的Text属性绑定到dictionary键,第二个绑定到dictionary值,而不使用gridview事件DataBound或类似方法 请举个例子,谢谢 <asp:GridView ID="uxContentByYearMonths" runat="server"> <Columns> <asp:TemplateField He

我使用asp.net 4 c#

我有一个gridview,在标签内。我想知道如何将第一个标签的
Text
属性绑定到dictionary键,第二个绑定到dictionary值,而不使用gridview事件
DataBound
或类似方法

请举个例子,谢谢

   <asp:GridView ID="uxContentByYearMonths" runat="server">
        <Columns>
            <asp:TemplateField HeaderText="Month">
            <ItemTemplate>
                <asp:Label ID="uxMonth" runat="server" Text='<%# Bind("Key") %>'></asp:Label>
            </ItemTemplate>

            </asp:TemplateField>
            <asp:TemplateField HeaderText="Total Content">
            <ItemTemplate>
                <asp:Label ID="uxCount" runat="server" Text='<%# Bind("Value") %>'></asp:Label>
            </ItemTemplate>
            </asp:TemplateField>
        </Columns>

// DataSource
Dictionary<int, int> result = new Dictionary<int, int>();

//数据源
字典结果=新字典();
字典结果=新建字典();
uxContentByYearMonths.DataSource=来自结果中的项
选择新{Key=item.Key,Value=item.Value};
uxContentByYearMonths.DataBind();

您可以从这里找到许多示例

考虑在GridView中使用ObjectDataSource。网上有很多关于它是什么以及如何使用它的例子。谢谢卢卡斯的建议
Dictionary<int, int> result = new Dictionary<int, int>();

uxContentByYearMonths.DataSource = from item in result 
                      select new { Key = item.Key, Value = item.Value };

uxContentByYearMonths.DataBind();