Webform GridView列表增删改查结合EF数据绑定

添加gridview数据到前端

 <form id="form1" runat="server">
        <div>

            <asp:GridView ID="GridView1" runat="server" Height="318px" Width="961px" AutoGenerateColumns="false"
                OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
                OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowCreated="GridView1_RowCreated">
                <Columns>
                    <asp:BoundField HeaderText="编号" DataField="Uid" ReadOnly="true" />
                    <asp:TemplateField HeaderText="姓名">
                        <ItemTemplate>
                            <asp:Label ID="lblName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"UserName") %>'>asp:Label>
                        ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"UserName") %>'>asp:TextBox>
                        EditItemTemplate>
                    asp:TemplateField>
                    <asp:CommandField HeaderText="操作" ShowEditButton="true" ShowCancelButton="true" ShowDeleteButton="true"
                        EditText="编辑" UpdateText="修改" DeleteText="<img style="text-decoration:none; border:0px;" src="images/delete.gif"onclick="JavaScript:return confirm ('确认删除吗?')" />" CancelText="取消" />
                Columns>
            asp:GridView>

            <asp:Button ID="btnAdd" runat="server" Text="新增" OnClick="btnAdd_Click" />
        div>
    form>

后端处理增删该查事件

  CEducationEntities context = new CEducationEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
            }
        }
        //绑定GridView数据
        private void DataBind()
        {
            List users = context.User.Where(m => m.Uid != string.Empty).ToList();
            this.GridView1.DataSource = users;
            this.GridView1.DataBind();
        }

        //编辑选中列表
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int index = e.NewEditIndex;
            GridView1.EditIndex = index;
            DataBind();
        }

        //修改数据和插入更新
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int index = e.RowIndex;
            GridViewRow rows = this.GridView1.Rows[index];
            string name = ((TextBox)rows.FindControl("txtName")).Text.Trim();
            string ID = rows.Cells[0].Text;
            if (!string.IsNullOrEmpty(ID) && ID != " ")
            {
                var sc = context.User.First(p => p.Uid.Equals(ID));
                sc.UserName = name;
                context.SaveChanges();
            }
            else
            {
                char[] pattern = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
                int n = pattern.Length;
                string result = "";
                Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
                for (int i = 0; i < 4; i++)
                {
                    int rnd = random.Next(0, n);
                    result += pattern[rnd];
                }
                User u = new User()
                {
                    Uid = Guid.NewGuid().ToString(),
                    Keyword = result,
                    UserName = ((TextBox)rows.FindControl("txtName")).Text.Trim(),
                    userLog = string.Empty
                };
                context.User.Add(u);
                context.SaveChanges();
            }
            this.GridView1.EditIndex = -1;
            DataBind();
        }

        //取消修改
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            DataBind();
        }

        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string strUid = GridView1.Rows[e.RowIndex].Cells[0].Text;
            User users = new User() { Uid = strUid };
            context.User.Attach(users);
            context.User.Remove(users);
            context.SaveChanges();
            DataBind();
        }

        //新增按钮
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            List list = context.User.Where(m => m.Uid != string.Empty).ToList();
            User firstUser = new User();
            list.Insert(list.Count, firstUser);
            this.GridView1.DataSource = list;
            this.GridView1.EditIndex = list.Count - 1;
            this.GridView1.DataBind();
        }

        //创建新行
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.EmptyDataRow)
            {
                e.Row.Cells[0].Enabled = false;
            }
        }

案例图片:

你可能感兴趣的:(webform相关知,webform,gridview)