How can I bind SortedList with GridView?

I have a custom object that provides me a SortedList collection which contains properties for say firstname, lastname. how can I bind my sorted list with GridView?

Following code shows how to bind sortedlist with GridView

Run sample code

<%@ Page Language="C#"%>
<%@ Import Namespace="System.Collections.Generic"%>
<script runat="server">
    
    protectedvoid Page_Load(object sender, EventArgs e)
    {
        
        SortedList<string,Member> mydata =new SortedList<string,Member>();
       mydata.Add("Tom", new Member("Tom", "Hanks"));
       mydata.Add("Harry", new Member("Harry", "Porter"));

       gridView.DataSource = mydata;
       gridView.DataBind();
    }

    publicclass Member
    {
        public Member(string name, string lastName)
        {
           this.name = name;
           this.lastName = lastName;
        }

        privatestring name;
        privatestring lastName;
        
        publicstring LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        publicstring Name
        {
           get { return name; }
           set { name = value; }
        }

    }
</script>
   
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       <asp:GridView runat="server" ID="gridView" AutoGenerateColumns="false">
           <Columns>
               <asp:TemplateField HeaderText="Key">
                   <ItemTemplate><%# Eval("Value.Name") %></ItemTemplate> 
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Name">
                   <ItemTemplate><%# Eval("Value.Name") %></ItemTemplate> 
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Last name">
                   <ItemTemplate><%# Eval("Value.LastName") %></ItemTemplate> 
               </asp:TemplateField>
           </Columns>
       </asp:GridView>
   </div>
   </form>
</body>
</html>
Bookmark with:
Technorati   Digg   Delicious   StumbleUpon   Facebook
My name is Jigar Desai I share my ideas on this site and you can contact me by filling contact form.

Categories