Tuesday, May 14, 2013

how to bind arraylist to gridview in asp.net

In previous articles we have seen how to bind the data to grid view, Bind data to dropdownlist,Bind data to dropdownlist in gridview,Export gridview to PDF.Here i would like to explain how to bind arraylist data to Grid view in asp.net.In the below i have used static array list for binding
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ArrayListToGridview.aspx.cs" Inherits="_ArrayListToGridview" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Bind ArrayList to Gridview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GvBindArrayList" runat="server"
AutoGenerateColumns="False" >
</asp:Gridview>
</form>
</body>
</html>

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using System.Collections.Generic;
using System.Linq;

public partial class _ArrayListToGridview : System.Web.UI.Page
{
protected void Page_Load(Object Sender, EventArgs E)
{
if (!IsPostBack) {
ArrayList Booknames = new ArrayList();
Booknames.Add ("Asp.net");
Booknames.Add ("C#.net");
Booknames.Add ("SharePoint");
Booknames.Add ("SqlServer");
Booknames.Add ("Jquery");
Booknames.Add ("Asp.netMVC");
GvBindArrayList.DataSource = Booknames;
GvBindArrayList.DataBind();
}
}
}

Post a Comment

Bel