In this article I will explain with an example, how to bind Arrays to GridView in ASP.Net with C# and VB.Net.
 
 
Binding One Dimensional Array
HTML Markup
The following HTML Markup consists of:
GridView: - For displaying One-Dimensional Array.
<asp:GridView ID="gv1D" runat="server"
    AutoGenerateColumns="true" Caption="1-Dimensional Array">
</asp:GridView>
 
 
Populate GridView using One Dimensional Array in C# and VB.Net
Inside the Page Load event handler, a One-Dimensional array is created and assigned to the DataSource property of the GridView and the GridView is populated.
C#
protected void Page_Load(object sender, EventArgs e)
{
    //1-Dimensional Array
    string[] arr1D = { "John Hammond", "Mudassar Khan", "Suzanne Mathews", "Robert Schidner" };
    gv1D.DataSource = arr1D;
    gv1D.DataBind();
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    '1-Dimensional Array
    Dim arr1D As String() = {"John Hammond", "Mudassar Khan", "Suzanne Mathews", "Robert Schidner"}
    gv1D.DataSource = arr1D
    gv1D.DataBind()
End Sub
 
 
Screenshot
Binding Arrays to GridView in ASP.Net
 
 
Binding Two Dimensional Array
HTML Markup
The following HTML Markup consists of:
GridView: - For displaying Two-Dimensional Array.
Columns
GridView consists of two BoundField columns.
<asp:GridView ID="gv2D" runat="server"
    AutoGenerateColumns="false" Caption="2-Dimensional Array">
    <Columns>
        <asp:BoundField DataField="Text" HeaderText="Name" />
        <asp:BoundField DataField="Value" HeaderText="Country" />
    </Columns>
</asp:GridView>
 
 
Populate GridView using Two-Dimensional Array in C# and VB.Net
Inside the Page Load event handler, a Two-Dimensional Array is created and an object of ArrayList class is created.
A FOR loop is executed over the Two-Dimensional Array and each item of the array is added to the object of ArrayList as a ListItem.
Finally, the object of Array List is assigned to the DataSource property of the GridView and the GridView is populated.
Note: When you try to bind more than one dimensional array to GridView it simply throws the following error.
          Hence, in order to bind two Dimensional arrays to GridView you will need to make use of ListItem and ArrayList.
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    //2-Dimensional Array
    string[,] arr2D = {
                        { "John Hammond", "United States" },
                        { "Mudassar Khan", "India" },
                        { "Suzanne Mathews", "France" },
                        { "Robert Schidner", "Russia" }
                  };
 
    ArrayList arrList = new ArrayList();
    for (int i = 0; i < arr2D.GetLength(0); i++)
    {
        arrList.Add(new ListItem(arr2D[i, 0], arr2D[i, 1]));
    }
    gv2D.DataSource = arrList;
    gv2D.DataBind();
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    '2-Dimensional Array
    Dim arr2D As String(,) = {
                                {"John Hammond", "United States"},
                                {"Mudassar Khan", "India"},
                                {"Suzanne Mathews", "France"},
                                {"Robert Schidner", "Russia"}
                            }
 
    Dim arrList As New ArrayList()
    For i As Integer = 0 To arr2D.GetLength(0)
        arrList.Add(New ListItem(arr2D(i, 0), arr2D(i, 1)))
    Next
    gv2D.DataSource = arrList
    gv2D.DataBind()
End Sub
 
 
Screenshot
Binding Arrays to GridView in ASP.Net
 
 
Binding Multi-Dimensional Array
HTML Markup
The following HTML Markup consists of:
GridView: - For displaying Multi-Dimensional Array.
Columns
GridView consists of four BoundField columns. 
<asp:GridView ID="gvMD" runat="server"
    AutoGenerateColumns="false" Caption="Multi-Dimensional Array">
    <Columns>
        <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
Populate GridView using Multi-Dimensional Array in C# and VB.Net
Inside the Page Load event handler, a Multi-Dimensional Array is created.
An object of DataTable class is created and columns are added with the same number of columns as in the Multi-Dimensional Array.
A FOR loop is executed over the Multi-Dimensional Array and each item from the array is added to the rows of DataTable object.
Finally, the DataTable is assigned to the DataSource property of the GridView and the GridView is populated.
C#
protected void Page_Load(object sender, EventArgs e)
{
    //Multi-Dimensional Array.
    string[,] arrMultiD = {
                            { "1", "John Hammond", "United States"},
                            { "2", "Mudassar Khan", "India"},
                            { "3", "Suzanne Mathews", "France"},
                            { "4", "Robert Schidner", "Russia"}
                      };
 
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[3] {
                        new DataColumn("CustomerId"),
                        new DataColumn("Name"),
                        new DataColumn("Country")
                    });
 
   for (int i = 0; i < arrMultiD.GetLength(0); i++)
   {
        dt.Rows.Add();
        dt.Rows[dt.Rows.Count - 1]["CustomerId"] = arrMultiD[i, 0];
        dt.Rows[dt.Rows.Count - 1]["Name"] = arrMultiD[i, 1];
        dt.Rows[dt.Rows.Count - 1]["Country"] = arrMultiD[i, 2];
    }
    gvMD.DataSource = dt;
    gvMD.DataBind();
}
 
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    'Multi-Dimensional Array.
    Dim arrMultiD As String(,) = {
                                    {"1", "John Hammond", "United States"},
                                    {"2", "Mudassar Khan", "India"},
                                    {"3", "Suzanne Mathews", "France"},
                                    {"4", "Robert Schidner", "Russia"}
                                }
    Dim dt As New DataTable()
    dt.Columns.AddRange(New DataColumn(2) {
                        New DataColumn("CustomerId"),
                        New DataColumn("Name"),
                        New DataColumn("Country")})
 
    For i As Integer = 0 To arrMultiD.GetLength(0) - 1
        dt.Rows.Add()
        dt.Rows(dt.Rows.Count - 1)("CustomerId") = arrMultiD(i, 0)
        dt.Rows(dt.Rows.Count - 1)("Name") = arrMultiD(i, 1)
        dt.Rows(dt.Rows.Count - 1)("Country") = arrMultiD(i, 2)
   Next
   gvMD.DataSource = dt
   gvMD.DataBind()
End Sub
 
 
Screenshot
Binding Arrays to GridView in ASP.Net
 
 
Downloads