关闭

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

标签: webformgridview
1243人阅读 评论(0) 收藏 举报
分类:

添加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="&lt;img style=&quot;text-decoration:none; border:0px;&quot; src=&quot;images/delete.gif&quot;onclick=&quot;JavaScript:return confirm ('确认删除吗?')&quot; /&gt;" 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<User> 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 != "&nbsp;")
            {
                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<User> 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;
            }
        }

案例图片:
这里写图片描述

这里写图片描述

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:11288次
    • 积分:585
    • 等级:
    • 排名:千里之外
    • 原创:48篇
    • 转载:4篇
    • 译文:1篇
    • 评论:0条