Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to bind multidimensional array to GridView in ASP.NET

Author: Richard Wand
Download Source Code : 1342_MultidimensionalArrayGridView.zip

This article will explain how we can bind multidimensional array to GridView in ASP.NET.

 

You can simply bind one dimensional and two dimensional arrays without using DataTable in your code but you have to create DataTable to bind multidimensional array to GridView using that DataTable. DataTable can be created dynamically by adding columns and rows to it.

  1. Create new website in Visual Studio 2010
  2. Add a GridView in aspx page of a web form

    <asp:GridView ID="GridView1" runat="server">
    </asp:GridView>

     
  3. We have to use following namespace in our code file

    C#
     
    using System.Data;
     
    VB.NET

    Imports System.Data
     
  4. Add following function in code file for CSharp or Visual Basic

    C#
     
    private DataTable BindMultidimentionalArray()
    {
        string[,] employees = {
                                           { "1", "Davolio", "Nancy", "5/1/1992", "Seattle", "USA" },
                                           { "2", "Fuller", "Andrew", "8/14/1992", "Tacoma", "USA" },
                                           { "3", "Peacock", "Margaret", "5/3/1993", "Redmond", "USA" },
                                           { "4", "Buchanan", "Steven", "10/17/1993", "London", "UK" },
                                           { "5", "Suyama", "Michael", "10/17/1993", "London", "UK" }
                                         };
     
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(string));
        table.Columns.Add("LastName", typeof(string));
        table.Columns.Add("FirstName", typeof(string));
        table.Columns.Add("HireDate", typeof(string));
        table.Columns.Add("City", typeof(string));
        table.Columns.Add("Country", typeof(string));
     
        for (int i = 0; i <= employees.GetUpperBound(0); i++)
        {
           table.Rows.Add();
           table.Rows[i]["ID"] = employees[i, 0];
            table.Rows[i]["LastName"] = employees[i, 1];
            table.Rows[i]["FirstName"] = employees[i, 2];
            table.Rows[i]["HireDate"] = employees[i, 3];
            table.Rows[i]["City"] = employees[i, 4];
            table.Rows[i]["Country"] = employees[i, 5];
     
        }
        return table;
    }
     
    VB.NET
     
    Private Function BindMultidimentionalArray() As DataTable
        Dim employees As String(,) = {
                                                        {"1", "Davolio", "Nancy", "5/1/1992", "Seattle", "USA"},
                                                        {"2", "Fuller", "Andrew", "8/14/1992", "Tacoma", "USA"},
                                                        {"3", "Peacock", "Margaret", "5/3/1993", "Redmond", "USA"},
                                                        {"4", "Buchanan", "Steven", "10/17/1993", "London", "UK"},
                                                        {"5", "Suyama", "Michael", "10/17/1993", "London", "UK"}
                                                      }
     
        Dim table As New DataTable()
        table.Columns.Add("ID", GetType(String))
        table.Columns.Add("LastName", GetType(String))
        table.Columns.Add("FirstName", GetType(String))
        table.Columns.Add("HireDate", GetType(String))
        table.Columns.Add("City", GetType(String))
        table.Columns.Add("Country", GetType(String))
     
        For i As Integer = 0 To employees.GetUpperBound(0)
            table.Rows.Add()
            table.Rows(i)("ID") = employees(i, 0)
            table.Rows(i)("LastName") = employees(i, 1)
            table.Rows(i)("FirstName") = employees(i, 2)
            table.Rows(i)("HireDate") = employees(i, 3)
            table.Rows(i)("City") = employees(i, 4)
            table.Rows(i)("Country") = employees(i, 5)
        Next
        Return table
    End Function
     
    Create an array of string and initialize it with data. Create an instance of DataTable and Add Columns to it with name and data type. We need to use a “for” loop to add rows to DataTable.
     
  5. Add Data Bound fields in GridView like below

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID"/>
            <asp:BoundField DataField="LastName" HeaderText="Last Name"/>
            <asp:BoundField DataField="FirstName" HeaderText="First Name"/>
            <asp:BoundField DataField="HireDate" HeaderText="Hire Date"/>
            <asp:BoundField DataField="City" HeaderText="City"/>
            <asp:BoundField DataField="Country" HeaderText="Country"/>
        </Columns>
    </asp:GridView>
     
    Add DataField attribute and HeaderText attribute for each BoundField. Set AutoGenerateColumns attribute of GridView control as false.
     
  6. Add code below in Page Load event to bind our function to GridView

    C#
     
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = BindMultidimentionalArray();
        GridView1.DataBind();
    }
     
    VB.NET

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        GridView1.DataSource = BindMultidimentionalArray()
        GridView1.DataBind()
    End Sub
     
  7. Now start debugging and see result in browser.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to change and update XML file in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net