Golf工作室

沧海中的一栗
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

纯代码实现GridView绑定增删改

Posted on 2009-09-02 09:22  Golf工作室  阅读(1018)  评论(2编辑  收藏  举报
2008-09-13 14:46 转载
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
        <asp:GridView ID="myGrid" runat="server" >
        </asp:GridView>
    </div>
    </form>
</body>
</html>

====================================

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//myself
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    private Employees.Employees_BLL bll = new Employees.Employees_BLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //设置GridView外观样式
            setGridViewStyle();

            //创建及设置Fields字段
            setFields();

            //设置 GridView 数据源绑定
            GridBind();
        }
        //以后台的方式添加 GridView 的各类事件
        myGrid.RowEditing += new GridViewEditEventHandler(myGrid_RowEditing);
        myGrid.RowUpdating += new GridViewUpdateEventHandler(myGrid_RowUpdating);
        myGrid.RowCancelingEdit += new GridViewCancelEditEventHandler(myGrid_RowCancelingEdit);
        myGrid.RowDeleting += new GridViewDeleteEventHandler(myGrid_RowDeleting);
    }

    方法#region 方法

    //设置GridView外观样式
    private void setGridViewStyle()
    {
        myGrid.AutoGenerateColumns = false;
        //设置Row的键值组成,具有唯一性
        string[] KeyNames = new string[] { "EmployeeID" };
        myGrid.DataKeyNames = KeyNames;

        //设置GridView属性
        myGrid.AllowPaging = true;    //设置分页
        myGrid.AllowSorting = true;    //设置排序
        myGrid.Font.Size = 10;        //设置字号大小
        myGrid.GridLines = GridLines.Both;    //设置网格线
        myGrid.PageSize = 15; //分页大小
        myGrid.PagerSettings.Position = PagerPosition.TopAndBottom; //分页位置
        myGrid.PagerStyle.HorizontalAlign = HorizontalAlign.Center; //分页对齐

        myGrid.HeaderStyle.BackColor = Color.Tan;
        myGrid.RowStyle.BackColor = Color.LightGoldenrodYellow;
        myGrid.AlternatingRowStyle.BackColor = Color.PaleGoldenrod;
        myGrid.HeaderStyle.ForeColor = Color.Black;
        myGrid.PagerStyle.BackColor = Color.Goldenrod;
        myGrid.SelectedRowStyle.BackColor = Color.LightBlue; //设置选择行背景颜色
        //myGrid.ShowFooter = true;
    }

    //创建及设置Fields字段
    private void setFields()
    {
        //创建"编辑"命令字段
        CommandField editField = new CommandField();
        editField.ButtonType = ButtonType.Button;
        editField.ShowEditButton = true; //显示"编辑"按钮
        editField.ShowCancelButton = true; //显示"取消"按钮
        editField.EditText = "编辑";
        editField.UpdateText = "更新";
        editField.CancelText = "取消";
        editField.ControlStyle.BackColor = Color.LightPink;
        editField.ItemStyle.Wrap = false;

        //创建"删除"命令字段
        CommandField deleteField = new CommandField();
        deleteField.ButtonType = ButtonType.Button;
        deleteField.ShowDeleteButton = true; //显示"删除"按钮
        deleteField.DeleteText = "删除";
        deleteField.ControlStyle.BackColor = Color.LightPink;
        deleteField.ItemStyle.Wrap = false;

        //创建数据绑定字段
        BoundField employeeidField = new BoundField();
        BoundField lastnameField = new BoundField();
        BoundField firstnameField = new BoundField();
        BoundField titleField = new BoundField();
        BoundField addressField = new BoundField();
        BoundField cityField = new BoundField();

        employeeidField.DataField = "EmployeeID";//指定数据源字段
        employeeidField.HeaderText = "员工代号";   //设置字段头名称
        employeeidField.ItemStyle.Wrap = false; //设置字段不换行
        employeeidField.ReadOnly = true;    //只读,编辑模式不能修改

        lastnameField.DataField = "LastName";
        lastnameField.HeaderText = "名字";
        lastnameField.ItemStyle.Wrap = false;
        lastnameField.ReadOnly = true;    //只读,编辑模式不能修改

        firstnameField.DataField = "FirstName";      
        firstnameField.HeaderText = "姓氏";
        firstnameField.ItemStyle.Wrap = false;
        //firstnameField.ReadOnly = true;

        titleField.DataField = "Title";
        titleField.HeaderText = "职称";
        titleField.ItemStyle.Wrap = false;

        addressField.DataField = "Address";
        addressField.HeaderText = "地址";
        addressField.ItemStyle.Wrap = false;

        cityField.DataField = "City";
        cityField.HeaderText = "城市";
        cityField.ItemStyle.Wrap = false;


        //将字段添加到GridView
        myGrid.Columns.Add(editField); //编辑
        myGrid.Columns.Add(deleteField);//删除
        myGrid.Columns.Add(employeeidField);
        myGrid.Columns.Add(lastnameField);
        myGrid.Columns.Add(firstnameField);
        myGrid.Columns.Add(titleField);
        myGrid.Columns.Add(addressField);
        myGrid.Columns.Add(cityField);
    }

    //设置 GridView 数据源绑定
    public void GridBind()
    {
        //bll = new Employees.Employees_BLL();
        myGrid.DataSource = bll.GetAllList();
        myGrid.DataBind();
    }

    #endregion 方法

    // GridView 编辑操作
    protected void myGrid_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //设置编辑行的索引
        myGrid.EditIndex = e.NewEditIndex;

        //设置更新与取消按钮之背景颜色
        myGrid.Columns[0].ControlStyle.BackColor = Color.LightSteelBlue;
        myGrid.Columns[1].ControlStyle.BackColor = Color.LightSteelBlue;
        myGrid.ShowFooter = true;
        //设置GridView在编辑模式时,TextBox字段宽度及背景颜色
        //EmployeeID字段
        myGrid.Columns[2].ControlStyle.Width = 80;
        myGrid.Columns[2].ControlStyle.BackColor = Color.LightBlue;
        myGrid.Columns[2].FooterText = "不可编辑";
        myGrid.Columns[2].FooterStyle.BackColor = Color.Red;
        //LastName字段
        myGrid.Columns[3].ControlStyle.Width = 80;
        myGrid.Columns[3].ControlStyle.BackColor = Color.LightBlue;
        myGrid.Columns[3].FooterText = "不可编辑";
        myGrid.Columns[3].FooterStyle.BackColor = Color.Red;
        //FirstName字段
        myGrid.Columns[4].ControlStyle.Width = 80;
        myGrid.Columns[4].ControlStyle.BackColor = Color.LightBlue;
        myGrid.Columns[4].FooterText = "可编辑";
        myGrid.Columns[4].FooterStyle.BackColor = Color.Red;
        //Title字段
        myGrid.Columns[5].ControlStyle.Width = 100;
        myGrid.Columns[5].ControlStyle.BackColor = Color.LightPink;
        myGrid.Columns[5].FooterText = "可编辑";
        myGrid.Columns[5].FooterStyle.BackColor = Color.Red;
        //Address字段
        myGrid.Columns[6].ControlStyle.Width = 120;
        myGrid.Columns[6].ControlStyle.BackColor = Color.LightPink;
        myGrid.Columns[6].FooterText = "可编辑";
        myGrid.Columns[6].FooterStyle.BackColor = Color.Red;
        //City字段
        myGrid.Columns[7].ControlStyle.Width = 80;
        myGrid.Columns[7].ControlStyle.BackColor = Color.LightGreen;
        myGrid.Columns[7].FooterText = "可编辑";
        myGrid.Columns[7].FooterStyle.BackColor = Color.Red;
        GridBind();
    }
    // GridView 更新操作
    protected void myGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (e.NewValues != e.OldValues)
        {
            Employees.Employees_Model model = new Employees.Employees_Model();
            model.EmployeeID = Convert.ToInt32(myGrid.Rows[e.RowIndex].Cells[2].Text.Trim());
            //Employees_DAL 中的 where 条件为 EmployeeID and LastName, 所以 LastName 不能更改
            model.LastName = myGrid.Rows[e.RowIndex].Cells[3].Text.ToString();
            model.FirstName = ((TextBox)myGrid.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString();
            model.Title = ((TextBox)myGrid.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString();
            model.Address = ((TextBox)myGrid.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString();
            model.City = ((TextBox)myGrid.Rows[e.RowIndex].Cells[7].Controls[0]).Text.ToString();
            //Employees.BLL.Employees_Model bll = new Employees.BLL.Employees_Model();
            bll.Update(model);

            //取消编辑时隐藏Footer
            myGrid.ShowFooter = false;
            //设置"编辑"和"删除"按钮还原为系统定义的颜色
            myGrid.Columns[0].ControlStyle.BackColor = Color.LightPink;
            myGrid.Columns[1].ControlStyle.BackColor = Color.LightPink;
            myGrid.EditIndex = -1;
            GridBind();
        }
    }
    // GridView 取消事件
    protected void myGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //取消编辑时隐藏Footer
        myGrid.ShowFooter = false;
        //设置"编辑"和"删除"按钮还原为系统定义的颜色
        myGrid.Columns[0].ControlStyle.BackColor = Color.LightPink;
        myGrid.Columns[1].ControlStyle.BackColor = Color.LightPink;
        myGrid.EditIndex = -1; //取消编辑状态
        GridBind();
    }
    // GridView 删除操作
    protected void myGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //设置更新与取消按钮之背景颜色
        myGrid.Columns[0].ControlStyle.BackColor = Color.LightSteelBlue;
        myGrid.Columns[1].ControlStyle.BackColor = Color.LightSteelBlue;
        // 获取 Employees_DAL 中的 where 条件: EmployeeID and LastName
        int strEmployeeID = Convert.ToInt32(myGrid.DataKeys[e.RowIndex].Values[0]);
        string strLastName = myGrid.Rows[e.RowIndex].Cells[3].Text.ToString();
        bll.Delete(strEmployeeID, strLastName); //删除
        GridBind();