自己先要建立一个名为bbs的数据库里面要建立一个名为linqtoTest的表。
在添加项目中添加linq to sql 类,再把资源管理器打开,新建连接,找到你建立的那个数据库。把那个表拖到
那个linq to sql 类中。就行了。
using System;using System.Data.SqlClient;using System.Data;using System.Linq;using System.Web.UI.WebControls;using System.Data.Linq;
public partial class linq_boundfield_ : System.Web.UI.Page{
linqtoTestDataContext db_Test = new linqtoTestDataContext(); protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) {
// getDatabind1(); getDatabind2(); }
} /// <summary> /// 这是用我们平常 /// 的方法来绑定数据 /// </summary> //void getDatabind1() //{
// SqlConnection con = DB.createsql(); // con.Open(); // string sql = "select * from LinqtoTest"; // SqlCommand cmd = new SqlCommand(sql, con); // SqlDataAdapter sda = new SqlDataAdapter(cmd); // DataSet ds = new DataSet(); // sda.Fill(ds, "linqtoTest"); // this.GridView1.DataSource = ds.Tables["linqtoTest"].DefaultView; // this.GridView1.DataBind(); //}
//这是用linq来绑定数据
void getDatabind2() { //linqtoTestDataContext db_Test = new linqtoTestDataContext(); this.GridView1.DataSource = db_Test.LinqtoTest; this.GridView1.DataKeyNames = new string[] { "Id" };
this.GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e) { int id = Convert.ToInt32(this.GridView1.DataKeys[e.RowIndex].Value);
Session["ID"] = id;
LinqtoTest entity = db_Test.LinqtoTest.First(p => p.Id == id);
entity.name = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text; entity.QQ = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text; entity.sex = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text; db_Test.SubmitChanges();
this.GridView1.EditIndex = -1; getDatabind2();
} protected void GridView1_RowDeleting(object sender, System.Web.UI.WebControls.GridViewDeleteEventArgs e) {
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value); // LinqtoTest test = db_Test.LinqtoTest.SingleOrDefault(p => p.Id == id); LinqtoTest test = db_Test.LinqtoTest.First(p => p.Id == id); //这和上面是一样的,找到符合条件的实例 db_Test.LinqtoTest.DeleteOnSubmit((from s in db_Test.LinqtoTest where s.Id == id select s).Single()); // db_Test.LinqtoTest.DeleteOnSubmit(test); //和上面也是一样 db_Test.SubmitChanges(); getDatabind2();
//在这里,应该知道的是 SingleOrDefault() Single() First() 都是从表中查找满足条件的实例, //不同的是First()是返回满足条件的第一个元素,不管你有多少个满足条件的,它只要一个,并且不发生异常。 //但是 SingleOrDefault() Single()这两个就不一样了,它们也是只返回一个满足条件的,但是要有多个满足条件的 //它们会发生异常。只不过SingleOrDefault(),当没有满足条件的时,它会返回默认值。
} protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e) { this.GridView1.EditIndex = e.NewEditIndex; getDatabind2();
} protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e) { this.GridView1.EditIndex = -1; getDatabind2(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {
for (int i = 0; i <= this.GridView1.Rows.Count; i++) { if (e.Row.RowType == DataControlRowType.DataRow) { //e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='Red'"); //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
//鼠标经过时,行背景色变 e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'"); //鼠标移出时,行背景色变 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
//e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00FF00'"); 当鼠标移开时还原背景色 //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
//记住:在GridView1_RowDataBound事件中写改变行色,添加确认的代码。 //this.style.backgroundColor要严格这样写,不能改的,Style不行,Backgroundcolor不行。
}
//这是删除进行确认时的操作
if (e.Row.Cells[5].HasControls()) {
LinkButton deletelbtn = e.Row.Cells[5].Controls[0] as LinkButton; if (deletelbtn.Text.Equals("删除")) deletelbtn.Attributes.Add("onclick", "return confirm('你确认要删除!')");
} }
/// <summary> /// 向数据库中添加新数据。 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnAdd_Click(object sender, EventArgs e) { if (JudgeExist()) { LinqtoTest newEntity = new LinqtoTest(); //if (txtQQ.Text.Trim() != null) //{ newEntity.QQ = this.txtQQ.Text.Trim(); //} //else { // Page.RegisterStartupScript("true", "<script>alert('不能为空!')</script>"); //}
newEntity.name = this.txtname.Text.Trim(); newEntity.sex = this.txtSex.Text.Trim(); db_Test.LinqtoTest.InsertOnSubmit(newEntity); db_Test.SubmitChanges(); getDatabind2(); clear(); } else { // Response.Write("<script>alert('此人已经存在!')</script>"); //这种方法要少用,用下面的比较好!
Page.RegisterStartupScript("true", "<script>alert('此人已经存在!')</script>"); } } /// <summary> /// 清空 /// </summary> void clear() { this.txtSex.Text = ""; this.txtQQ.Text = ""; this.txtname.Text = "";
}
/// <summary> /// 判断是否有同名的人,若有则不能添加。 /// </summary> /// <returns></returns> bool JudgeExist() { string name = this.txtname.Text; if (db_Test.LinqtoTest.SingleOrDefault(p => p.name == name) != null) { return false; } return true;
}
// 如果ItemTemplate列时,只是在更改时有所不同。 // (1)如果用的是TemplateField绑定的: //点击编辑时,若要取得更新的信息,要用下面的方法: //entity.QQ= ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text //(2)如果用的是boundfield绑定的 //点击编辑时,若要取得更新的信息,要用下面的方法: //entity.QQ = ((TextBox)(this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
}
本文来自博客,转载请标明出处:http://blog.csdn.net/Apenghui/archive/2009/10/11/4640271.aspx