예제 #1
0
    //保存修改
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        var id = Guid.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

        using (var context = new DataContext.StuDBContext())
        {
            //查询出要修改这条记录
            var p = context.Students.Find(id);
            //读出gridview中用户编辑的字段,给每个允许修改的实体属性赋值
            //获取用户编辑的这一行
            var row   = GridView1.Rows[e.RowIndex];
            var sn    = (row.Cells[0].Controls[0] as TextBox).Text.Trim();
            var name  = (row.Cells[1].Controls[0] as TextBox).Text.Trim();
            var phone = (row.Cells[2].Controls[0] as TextBox).Text.Trim();

            var department = Guid.Parse(((DropDownList)row.FindControl("editdepartment")).SelectedValue);
            p.StudentCode = sn;
            p.Name        = name;
            p.Phone       = phone;
            p.Department  = context.Departments.Find(department);
            context.SaveChanges();
        }
        GridView1.EditIndex = -1;
        _getData();
    }
예제 #2
0
    //保存修改
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //查询出该记录的主键
        var id = Guid.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

        using (var context = new DataContext.StuDBContext())
        {
            var s = context.Students.Find(id);
            //读出Gridview中用户编辑的字段,给每个允许修改的实体属性赋值
            //获取用户编辑的这一行
            var row          = GridView1.Rows[e.RowIndex];
            var sn           = (row.Cells[0].Controls[0] as TextBox).Text.Trim();
            var name         = (row.Cells[1].Controls[0] as TextBox).Text.Trim();
            var add          = (row.Cells[2].Controls[0] as TextBox).Text.Trim();
            var Departmentid = Guid.Parse(((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DdlDepartment")).SelectedValue);
            s.Department = context.DepartMents.Find(Departmentid);
            s.StudentNo  = sn;
            s.FullName   = name;
            s.Address    = add;



            context.SaveChanges();
        }
        GridView1.EditIndex = -1;
        _getData();
    }
예제 #3
0
    //删除
    private void NewMethod(GridViewDeleteEventArgs e)
    {
        var id = Guid.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

        using (var context = new DataContext.StuDBContext())
        {
            var delstu = context.Students.Find(id);
            context.Students.Remove(delstu);
            context.SaveChanges();
        }
    }
예제 #4
0
    //删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        var id = Guid.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

        using (var context = new DataContext.StuDBContext())
        {
            //删除这条记录
            var delstudent = context.Students.Find(id);
            context.Students.Remove(delstudent);
            context.SaveChanges();
        }
        _getData();
    }