LINQ中的数据操作(删除,更新)

以下的方法可以用在gridview控件中,代码如下:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        binderGridView();
    }

//数据绑定
    public void binderGridView()
    {
        var stutdent = new linqtestDataContext();
        var linqString = from students in stutdent.Student
                         select
                         new
                         {
                             students.S_sum ,
                             students.S_name ,
                             students.S_birth
                                
                         };
        this.GridView1.DataSource = linqString;
        this.GridView1.DataBind();
    }

//数据删除
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
        var st = new linqtestDataContext();
        var  linqstring = from students in st.Student
                            where students.S_sum == id
                            select students;
        st.Student.DeleteAllOnSubmit(linqstring);
        st.SubmitChanges();
        binderGridView();

       
    }

//数据编辑取消
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        binderGridView();
    }

//数据更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string ssum = GridView1.DataKeys[e.RowIndex].Value.ToString();
        string sname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Replace("'", " ").ToString();
        string sbirth = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Replace("'", " ").ToString();
        var st = new linqtestDataContext();
        var linqstring = from student in st.Student
                         where student.S_sum == ssum
                         select student;
       
        foreach (Student s in linqstring)
        {
            s.S_name = sname;
            s.S_birth = sbirth;
        }
        st.SubmitChanges();
        GridView1.EditIndex = -1;
        binderGridView();

    }


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cxcco/archive/2009/05/28/4222039.aspx

posted @ 2009-06-18 15:02  Libo@Deng  阅读(492)  评论(0编辑  收藏  举报