Bind an ArrayList with Hyperlinks to a GridView

A very frequently asked question in the forums is that of binding an ArrayList with Hyperlinks to an ASP.NET GridView control. Here's how to do so:

Declare a GridView in the following manner:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">


    <asp:HyperLinkField DataNavigateUrlFields="Text" DataTextField="Value" />



Notice that we have declared a <asp:Hyperlink> field and set the DataNavigateUrlFields to "Text" and the DataTextFields set to "Value". The AutoGenerateColumns property of the GridView is set to False.

Now add the following code that binds the ArrayList with Hyperlinks to the GridView


        ArrayList aList = new ArrayList();

        aList.Add(new ListItem(""));

        aList.Add(new ListItem(""));       

        aList.Add(new ListItem(""));

        aList.Add(new ListItem(""));

        GridView1.DataSource = aList;



        Dim aList As New ArrayList()

        aList.Add(New ListItem(""))

        aList.Add(New ListItem(""))

        aList.Add(New ListItem(""))

        aList.Add(New ListItem(""))

        GridView1.DataSource = aList


Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

No comments: