【问题标题】:How to dynamic bind a Control in a GridView如何在 GridView 中动态绑定控件
【发布时间】:2011-10-17 18:05:07
【问题描述】:

我使用 asp.net 4 c#。

我有一个网格视图,位于标签内。我想知道如何将第一个标签的 Text 属性绑定到字典 KEY 并将第二个标签绑定到字典 VALUE,而不使用 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>();

【问题讨论】:

  • 考虑为您的 GridView 使用 ObjectDataSource。网络上有很多关于它是什么以及如何使用它的示例。
  • 感谢卢卡萨斯的建议

标签: c# asp.net gridview


【解决方案1】:
Dictionary<int, int> result = new Dictionary<int, int>();

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

uxContentByYearMonths.DataBind();

你可以从这里找到很多例子Bind NameValueCollection to GridView?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多