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 objects to GridView control in ASP.NET

Author: Rudy Pinson
Download Source Code : 1332_BindObjectsToGridView.zip

I will show you in this article that how you can bind objects to GridView control in ASP.NET.

 

First, I will create a Persons class in this example. Then I will create 3 objects for this class and populate these objects with data. At the end, I will bind these objects to GridView Control.
 
  1. Create a new Web Site in Visual Studio 2010 either in C# or VB.NET
  2. Add a Web Form to Web Site
  3. Add a class to Web Site and rename it as Persons
  4. Write code below in Persons class

    C#
     
    public class Persons
    {
        private int _personID = 0;
        private string _lastName = "";
        private string _firstName = "";
        private string _city = "";
        private string _state = "";
     
        public int PersonID
        {
            set
            {
                this._personID = value;
            }
            get
            {
                return this._personID;
            }
        }
        public string LastName
        {
            set
            {
                this._lastName = value;
            }
            get
            {
                return this._lastName;
            }
        }
        public string FirstName
        {
            set
            {
                this._firstName = value;
            }
            get
            {
                return this._firstName;
            }
        }
        public string City
        {
            set
            {
                this._city = value;
            }
            get
            {
                return this._city;
            }
        }
        public string State
        {
            set
            {
                this._state = value;
            }
            get
            {
                return this._state;
            }
        }
     
        public static Persons[] GetPersons()
        {
            Persons person1 = new Persons();
            person1.PersonID = 1;
            person1.LastName = "White";
            person1.FirstName = "Johnson";
            person1.City = "Menlo Park";
            person1.State = "CA";
     
            Persons person2 = new Persons();
            person2.PersonID = 2;
            person2.LastName = "Smith";
            person2.FirstName = "Meander";
            person2.City = "Lawrence";
            person2.State = "KS";
     
            Persons person3 = new Persons();
            person3.PersonID = 3;
            person3.LastName = "Locksley";
            person3.FirstName = "Charlene";
            person3.City = "San Francisco";
            person3.State = "CA";
     
            Persons[] persons = new Persons[3];
            persons[0] = person1;
            persons[1] = person2;
            persons[2] = person3;
            return persons;
     
        }
    }
     
    VB.NET
     
    Public Class Persons
        Private _personID As Integer = 0
        Private _lastName As String = ""
        Private _firstName As String = ""
        Private _city As String = ""
        Private _state As String = ""
     
        Public Property PersonID() As Integer
            Get
                Return Me._personID
            End Get
            Set(ByVal value As Integer)
                Me._personID = value
            End Set
        End Property
        Public Property LastName() As String
            Get
                Return Me._lastName
            End Get
            Set(ByVal value As String)
                Me._lastName = value
            End Set
        End Property
        Public Property FirstName() As String
            Get
                Return Me._firstName
            End Get
            Set(ByVal value As String)
                Me._firstName = value
            End Set
        End Property
        Public Property City() As String
            Get
                Return Me._city
            End Get
            Set(ByVal value As String)
                Me._city = value
            End Set
        End Property
        Public Property State() As String
            Get
                Return Me._state
            End Get
            Set(ByVal value As String)
                Me._state = value
            End Set
        End Property
     
        Public Shared Function GetPersons() As Persons()
            Dim person1 As New Persons()
            person1.PersonID = 1
            person1.LastName = "White"
            person1.FirstName = "Johnson"
            person1.City = "Menlo Park"
            person1.State = "CA"
     
            Dim person2 As New Persons()
            person2.PersonID = 2
            person2.LastName = "Smith"
            person2.FirstName = "Meander"
            person2.City = "Lawrence"
            person2.State = "KS"
     
            Dim person3 As New Persons()
            person3.PersonID = 3
            person3.LastName = "Locksley"
            person3.FirstName = "Charlene"
            person3.City = "San Francisco"
            person3.State = "CA"
     
            Dim persons As Persons() = New Persons(2) {}
            persons(0) = person1
            persons(1) = person2
            persons(2) = person3
            Return persons
     
        End Function
    End Class
     
    First I have declared private variables then I have defined public properties for each private variable. Then I have written a function which returns array of Persons. I have created three objects for Persons class and populated with data for each property of every object. At the end, I have created an array of Persons which will contain 3 objects of Persons class. Then I have added 3 objects to array.
     
  5. Open Default.aspx page and drag and drop a GridView control
     
     
    <asp:GridView ID="GridView1" runat="server">
     </asp:GridView> 
     
  6. Open Code behind file and write code below in Page Load event

    C#
     
    protected void Page_Load(object sender, EventArgs e)
    {
        Persons[] personsData = Persons.GetPersons();
        GridView1.DataSource = personsData;
        GridView1.DataBind();
    }
     
     
    VB.NET
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim personData As Persons() = Persons.GetPersons()
        GridView1.DataSource = personData
        GridView1.DataBind()
    End Sub
    Call the GetPersons() function of Persons class and assign data to array of Persons. Then bind data to GridView control.
     
  7. Now View the Web Site in browser

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to check if two files are equal in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net