0

I use asp.net 4 c#.

I have a gridview, inside a Label. I would like to know how to BIND the Text property for the first label to the dictionary KEY and the second to dictionary VALUE, without using the gridview event DataBound or similar.

Please suggest an example, thanks

   <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>();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
GibboK
  • 71,848
  • 143
  • 435
  • 658

1 Answers1

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();

you can find many examples from here Bind NameValueCollection to GridView?

Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153