Home .NET Controls Gridview ObjectDataSource
Author:

We use objectDataSource as a middle tier to retrieve and update data. It is often use in conjunction with the data-bound controls such as FormView, Gridview or Details View.

In three-tier client server architecture we would normally have the database layer, business logic layer and presentation layer. The business logic layer is essentially the layer we interact with the ObjectDataSource.

Below is sample code how we can use gridview objectdatasource.

SAMPLE DATA CLASS

sing System;
using System.Collections.Generic;
using System.Text; 

namespace QVISLib

{

    public class SampleData

    {

        public IList<Student> GetStudents()

        {

            IList<Student> students = new List<Student>();

 

            Student student = new Student("Bob", 20, 10, 10,10,10, 10);

            students.Add(student);

 

            student = new Student("Jane", 20, 20, 20, 20, 20, 20);

            students.Add(student);

 

            student = new Student("Mickey", 30, 30, 30, 30, 30, 30);

            students.Add(student);

 

            student = new Student("Ben", 40, 40, 40, 40, 40, 40);

            students.Add(student);

 

            student = new Student("Thomas", 50, 50, 55, 53, 52, 55);

            students.Add(student);

 

            student = new Student("Frank", 66, 64, 66, 6, 60, 60);

            students.Add(student);

 

            student = new Student("Martin", 7, 70, 70, 70, 70, 70);

            students.Add(student);

 

            student = new Student("Bailey", 80, 80, 80, 80, 80, 80);

            students.Add(student);

 

            student = new Student("John", 90, 90, 90, 90, 90, 90);

            students.Add(student);

 

            student = new Student("Andrew", 78, 78, 78, 78, 78, 78);

            students.Add(student);

 

            student = new Student("Stacey", 99, 99, 99, 99, 99, 99);

            students.Add(student);

 

            student = new Student("Julie", 12, 12, 12, 12, 21, 12);

            students.Add(student);

 

            return students;

        }       

    }

  

 

    public class Student

    {

        public Student(string name, int age, int marksInMaths, int marksInEnglish, int marksInScience, 

            int totalMarks, int obtainedMarks )

        {

            _name = name;

            _age = age;

            _marksInMaths = marksInMaths;

            _marksInEnglish = marksInScience;

            _marksInScience = marksInScience;

            _totalMarks = totalMarks;

            _obtainedMarks = obtainedMarks;

        }

 

        // fields contained in Student class

 

        #region VARIABLES

 

        string _name;

        int _age;

        int _marksInMaths;

        int _marksInEnglish;

        int _marksInScience;

        int _totalMarks = 300;    // initialization

        int _obtainedMarks;

 

        #endregion

 

        #region PROPERTIES

 

        public string Name

        {

            get { return _name; }

            set { _name = value; }

        }       

 

        public int Age

        {

            get { return _age; }

            set { _age = value; }

        }       

 

        public int MarksInMaths

        {

            get { return _marksInMaths; }

            set { _marksInMaths = value; }

        }

 

        public int MarksInEnglish

        {

            get { return _marksInEnglish; }

            set { _marksInEnglish = value; }

        }

 

        public int MarksInScience

        {

            get { return _marksInScience; }

            set { _marksInScience = value; }

        }       

 

        public int TotalMarks

        {

            get { return _totalMarks; }

            set { _totalMarks = value; }

        }       

 

        public int ObtainedMarks

        {

            get { return _obtainedMarks; }

            set { _obtainedMarks = value; }

        }

        double _percentage;

 

        #endregion

    }

}

 

ObjectDataSource and Gridview in Action

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="DGridview.aspx.cs" Inherits="_DGridview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>GridView Demo</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <h1>GridView Demo</h1>

 

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

                DataSourceID="ObjectDataSource1">

        <Columns>

            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

            <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />

            <asp:BoundField DataField="MarksInMaths" HeaderText="MarksInMaths"

                SortExpression="MarksInMaths" />

            <asp:BoundField DataField="MarksInEnglish" HeaderText="MarksInEnglish"

                SortExpression="MarksInEnglish" />

            <asp:BoundField DataField="MarksInScience" HeaderText="MarksInScience"

                SortExpression="MarksInScience" />

            <asp:BoundField DataField="TotalMarks" HeaderText="TotalMarks"

                SortExpression="TotalMarks" />

            <asp:BoundField DataField="ObtainedMarks" HeaderText="ObtainedMarks"

                SortExpression="ObtainedMarks" />

        </Columns>

    </asp:GridView>

 

    </div>

        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"

            SelectMethod="GetStudents" TypeName="QVISLib.SampleData">

        </asp:ObjectDataSource>

    </form>

 

</body>

</html>

OUTPUT

gridview-objectdatasource



Comments (0)
Write comment
Your Contact Details:
Comment:
Security
Please input the anti-spam code that you can read in the image.

"