Binding Grid View Control

 Rate It (0)

In this article I will sharing how to bind with GridView control with Database and without database using Data Table.

Bind GridView with Database

I have define the GridView in the Web Form (Presentation Layer) and from this I am calling the method defined in BLL class (Business Logic Layer)

 

Here I am calling the method from presentation layer which was defined in BLL class (Business Login Layer)

Calling the BindEmpData class from the Web Form.
grdvTest.DataSource=BLL.BindEmpData(intEmpId);
grdvTest.DataBind();

Method in the BLL class and it is calling the other class in the Data Access Layer.
public static DataSet BindPlanData(int EmpId)
    {
        DataSet ds = new DataSet();
        ds = Dsll. GetEmployeeInfo(EmpId);
        return ds;
    }

Method defined in the DAL class

    public static DataSet GetEmployeeInfo(int intEmpId)
        {
            DataSet ds;
            int QType = 2;
            try
            {
                using (SqlConnection oConnection = new SqlConnection(ApplicationConnectionString()))
                {
                    SqlParameter[] parameters = new SqlParameter[1];

                    parameters[0] = new SqlParameter(intEmpId, System.Data.SqlDbType.Int);
                    parameters[0].Value = intEmpId

                    ds = SqlHelper.ExecuteDataset(oConnection, CommandType.StoredProcedure, "mySP_GetEmployeeInfo", parameters);
                }
                return ds;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message + " GetEmployeeInfo", ex);
            }
        }

 


Bind GridView without Database using Data Table

void connectGrid()
    {
DataTable dt = new DataTable();
        dt.Columns.Add("EmpId");
        dt.Columns.Add("EmpName");
        dt.Columns.Add("EmpTel");

        dt.Rows.Add("10023", "Abdullah Khan", "882-2221");
        dt.Rows.Add("11002", "Abulrehman Ali", "882-1132");
        dt.Rows.Add("23211", "Asim Afzal", "KG", "882-4211");

        grdvTest.DataSource = dt;
        grdvTest.DataBind();
}

Revision number 1, Wednesday, May 27, 2009 12:27:45 AM by Asim Afzal

Comments

Related Articles

Data Access

There's a lot of great information on the net about accessing data in common data access patterns with ASP.NET 2.0. Videos SQL Server Videos - If you're just learning about SQL Server, start with these beginner videos. How Do I: Access Data with LINQ

Extending the GridView control

Extending Grid View Control of ASP .NET If you want to modify some functionality of existing Grid View control of ASP .Net then you always have the option of extending the control. In this article I have extend the Grid View with custom field. To extend the

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (14)
  2. codehard (3)
  3. Babunareshnarra (2)
  4. Dungimon (1)
  5. cabhilash (1)
Microsoft Communities