用gricView实现常用的操作 选择啊 编辑啊 之类的
注意数据访问是用的EntityFrameWork
为了方便 页面代码这里我就不贴了,免得占地方
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.Objects; using BosloyERPModel; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Session["valiCode"] != null) Response.Write(Session["valiCode"]); GridView1.DataSource = new BslDB().departInfo; GridView1.DataBind(); } } protected void Button1_Click(object sender, EventArgs e) { //MembershipUser user= Membership.GetUser(); ////User.Identity.Name = "xx"; //string name = User.Identity.Name; //Response.Write(user.UserName); BslDB bsl = new BslDB(); departInfo depart = new departInfo(); depart.departName = depName.Text; depart.telphone = phone.Text; depart.leader = person.Text; bsl.AddTodepartInfo(depart); int result = bsl.SaveChanges(); GridView1.DataSource = bsl.departInfo; GridView1.DataBind(); } protected void GridView1_SelectedIndexChanging1(object sender, GridViewSelectEventArgs e) { ////GridView1.Rows[e.NewSelectedIndex].dataf //GridView1.DataKeys[e.NewSelectedIndex].Value; if(e.NewSelectedIndex!=-1) Response.Write("xxx" + GridView1.DataKeys[e.NewSelectedIndex].Value); int id = Convert.ToInt32( GridView1.DataKeys[e.NewSelectedIndex].Value); var de = new BslDB().departInfo.Where(c => c.id ==id ).First() ; depName.Text = de.departName; phone.Text = de.telphone; person.Text = de.leader; ViewState["curDep"] = id.ToString(); Response.Write(de.departName); } protected void Button2_Click(object sender, EventArgs e) { BslDB db=new BslDB(); var de = db.departInfo.Where(c => c.id == 8).First(); de.departName = "yy俱乐部"; db.SaveChanges(); } }
最终效果: