65.9K
CodeProject 正在变化。 阅读更多。
Home

绑定GridView控件

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0投票)

2013 年 10 月 11 日

CPOL

1分钟阅读

viewsIcon

9431

在本文中,我将分享如何使用数据库以及使用数据表在不使用数据库的情况下将 GridView 控件绑定到数据。使用数据库绑定 GridView

在本文中,我将分享如何使用数据库以及使用数据表在不使用数据库的情况下将 GridView 控件绑定到数据。

使用数据库绑定 GridView

我已在 Web Form(表示层)中定义了 GridView,并从这里调用 BLL 类(业务逻辑层)中定义的方法。

 

在这里,我从表示层调用在 BLL 类(业务逻辑层)中定义的方法。

从 Web Form 调用 BindEmpData 类。
grdvTest.DataSource=BLL.BindEmpData(intEmpId);
grdvTest.DataBind();

BLL 类中的方法,它正在调用数据访问层中的另一个类。
public static DataSet BindPlanData(int EmpId)
    {
        DataSet ds = new DataSet();
        ds = Dsll. GetEmployeeInfo(EmpId);
        return ds;
    }

DAL 类中定义的方法

    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);
            }
        }

 


使用数据表在不使用数据库的情况下绑定 GridView

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();
}

© . All rights reserved.