纯代码实现GridView绑定增删改

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>

<%@ 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();

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { getData(); } } //得到数据 public void getData() { this.GridView1.DataSource = BLL.mesManager.getGridData(); this.GridView1.DataBind(); } // 绑定之后发生 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton lbtn = (LinkButton)e.Row.FindControl("LinkButton2"); e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'"); e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor"); string aa = e.Row.Cells[3].Text.ToString(); lbtn.Attributes.Add("OnClick", "return confirm('您确认要删除吗?')"); } } //发生时间时激发 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "del") { int id = Convert.ToInt32(e.CommandArgument); int resultNum = BLL.mesManager.del(id); if (resultNum > 0) { Response.Write("<script>alert('删除成功');</script>"); getData(); } else { Response.Write("<script>alert('删除失败');</script>"); } } else if (e.CommandName == "upd") { int id = Convert.ToInt32(e.CommandArgument); Response.Redirect("Update.aspx?id=" + id); } } protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { } //pageindexchangeing protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { this.GridView1.PageIndex = e.NewPageIndex; getData(); } //全部删除 protected void Button1_Click(object sender, EventArgs e) { for (int i = 0; i <= GridView1.Rows.Count - 1; i++) { CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1"); if (cbox.Checked == true) { int id=Convert.ToInt32( this.GridView1.Rows[i].Cells[1].Text); BLL.mesManager.del(id); } } getData(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值