In this article I will explain with an example, how to bind (populate) GridView control using Array, ArrayList and Generic List in ASP.Net using C# and VB.Net.
This article will illustrate how to bind (populate) GridView control using string Array, ArrayList and Generic List of string in ASP.Net.
HTML Markup
The following HTML Markup consists of an ASP.Net GridView control. Since the Array, ArrayList or Generic List does not have any named DataItem hence instead of Eval or Bind Container.DataItem is used.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Customer Names">
<ItemTemplate>
<%# Container.DataItem %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
You will need to import the following namespace.
C#
using System.Collections;
VB.Net
Imports System.Collections
Bind (Populate) GridView control using Array in ASP.Net
Inside the Page Load event of the page, an Array is populated with some string values which ultimately is used to populate the GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Populate GridView using Array of string.
string[] arrNames = new string[4];
arrNames[0] = "John Hammond";
arrNames[1] = "Mudassar Khan";
arrNames[2] = "Suzanne Mathews";
arrNames[3] = "Robert Schidner";
GridView1.DataSource = arrNames;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
'Populate GridView using Array of string.
Dim arrNames As String() = New String(3) {}
arrNames(0) = "John Hammond"
arrNames(1) = "Mudassar Khan"
arrNames(2) = "Suzanne Mathews"
arrNames(3) = "Robert Schidner"
GridView1.DataSource = arrNames
GridView1.DataBind()
End If
End Sub
Bind (Populate) GridView control using ArrayList in ASP.Net
Inside the Page Load event of the page, an ArrayList is populated with some string values which ultimately is used to populate the GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Populate GridView using ArrayList.
ArrayList alstNames = new ArrayList();
alstNames.Add("John Hammond");
alstNames.Add("Mudassar Khan");
alstNames.Add("Suzanne Mathews");
alstNames.Add("Robert Schidner");
GridView1.DataSource = alstNames;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
'Populate GridView using ArrayList.
Dim alstNames As New ArrayList()
alstNames.Add("John Hammond")
alstNames.Add("Mudassar Khan")
alstNames.Add("Suzanne Mathews")
alstNames.Add("Robert Schidner")
GridView1.DataSource = alstNames
GridView1.DataBind()
End If
End Sub
Bind (Populate) GridView control using Generic List in ASP.Net
Inside the Page Load event of the page, a Generic List is populated with some string values which ultimately is used to populate the GridView control.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Populate GridView using List of string.
List<string> lstNames = new List<string>();
lstNames.Add("John Hammond");
lstNames.Add("Mudassar Khan");
lstNames.Add("Suzanne Mathews");
lstNames.Add("Robert Schidner");
GridView1.DataSource = lstNames;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
'Populate GridView using List of string.
Dim lstNames As New List(Of String)()
lstNames.Add("John Hammond")
lstNames.Add("Mudassar Khan")
lstNames.Add("Suzanne Mathews")
lstNames.Add("Robert Schidner")
GridView1.DataSource = lstNames
GridView1.DataBind()
End If
End Sub
Screenshot
Demo
Downloads