dataGrid 编辑 更新 删除 取消

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using 图书管理系统.CommonHelper;
using Model;
using BLL;

namespace 图书管理系统.admin
{
    public partial class managereaders : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //#region 判断Session是否存在,如果不存在,提示并跳转到登陆页面
            //if (Session["admin"] == null)
            //{
            //    Common.Alert(this.Page, "请先登陆!");
            //    HttpContext.Current.Response.Write("<script type='text/javascript'>window.parent.parent.location.href='login.aspx';</script>");
            //    return;
            //}
            //#endregion
            if (!Page.IsPostBack)
            {
                #region 添加查询条件

                dropQuery.Items.Add("读者号");
                dropQuery.Items.Add("姓名");
                dropQuery.Items.Add("电话");
                dropQuery.Items.Add("邮箱");
                dropQuery.Items.Add("读者类型");

                #endregion
            }
            
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }

        void Bind() 
        {
            //定义所需的字符串
            string str = dropQuery.SelectedValue;
            string filedName = "";
            string value;
            //根据用户选择的方式传参数
            switch (str)
            {
                //case ("图书证号"):
                //    filedName = "libraryCardID=@0";
                //    break;
                case ("读者号"):
                    filedName = "readerID=@0";
                    break;
                case ("姓名"):
                    filedName = "name=@0";
                    break;
                case ("电话"):
                    filedName = "phone=@0";
                    break;
                case ("邮箱"):
                    filedName = "email=@0";
                    break;
                case ("读者类型"):
                    filedName = "readerType=@0";
                    break;
            }
            //实例化
            Readers enReaders = new Readers();
            ReadersBLL readersBLL = new ReadersBLL();
            List<Readers> Treaders = new List<Readers>();
            //查出读者信息
            value = txtQuery.Text.Trim();
            Treaders = readersBLL.QueryByAllLetter(filedName, value);
            //给表格绑定数据源
            dgManageReader.DataSource = Treaders;
            dgManageReader.DataBind();
        }

        protected void Edit(Object sender,DataGridCommandEventArgs e)
        {
            dgManageReader.EditItemIndex = (int)e.Item.ItemIndex;
            Bind();
        }

        protected void Delete(Object sender, DataGridCommandEventArgs e)
        {
            Readers enReaders = new Readers();
            ReadersBLL readersBLL = new ReadersBLL();

            //string ID = (string)dgManageReader.DataKeys[(int)e.Item.ItemIndex-1];
            enReaders.readerID = e.Item.Cells[0].Text;
            enReaders.isValuable = "false";
            bool flag = readersBLL.DeleteReader(enReaders);
            if (flag)
            {
                Common.Alert(this.Page,"读者删除成功");
                Bind();
            }

        }

        protected void Cancel(Object sender, DataGridCommandEventArgs e)
        {
            dgManageReader.EditItemIndex = -1;
            Bind(); 
        }

        protected void Update(Object sender, DataGridCommandEventArgs e)
        {
            Readers enReaders = new Readers();
            ReadersBLL readersBLL = new ReadersBLL();

            //string ID = (string)dgManageReader.DataKeys[(int)e.Item.ItemIndex];
            //enReaders.readerID = ((TextBox)e.Item.Cells[0].Controls[0]).Text;
            enReaders.readerID = e.Item.Cells[0].Text;
            enReaders.name = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
            enReaders.phone = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
            enReaders.email = ((TextBox)e.Item.Cells[3].Controls[0]).Text;

            bool flag = readersBLL.UpdateReader(enReaders);
            if (flag)
            {
                Common.Alert(this.Page,"更新成功");
                //TODO:郑浩-在更新成功后,是文本框变为原来的样子
                dgManageReader.EditItemIndex = -1;
                Bind(); 
               
              
            }
        }
    }
}


你可能感兴趣的:(datagridview)