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
作者:Libo@Deng
出处:http://www.cnblogs.com/cxcco/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
我的微博:
新浪微博