前台代码: <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > < html xmlns ="http://www.w3.org/1999/xhtml" > < head runat ="server" > < title > GridView+Linq增删改操作 </ title > </ head > < body > < form id ="form1" runat ="server" > < div > < asp:GridView ID ="gvDictionary" runat ="server" SkinID ="gvDefault" OnRowCancelingEdit ="gvDictionary_RowCancelingEdit" OnRowEditing ="gvDictionary_RowEditing" OnRowUpdating ="gvDictionary_RowUpdating" OnRowDeleting ="gvDictionary_RowDeleting" onpageindexchanging ="gvDictionary_PageIndexChanging" onrowdatabound ="gvDictionary_RowDataBound" onsorting ="gvDictionary_Sorting" AllowPaging ="True" AllowSorting ="True" AutoGenerateColumns ="False" CellPadding ="4" ForeColor ="#333333" GridLines ="None" PageSize ="5" Width ="419px" > < FooterStyle BackColor ="#1C5E55" Font-Bold ="True" ForeColor ="White" /> < RowStyle BackColor ="#E3EAEB" /> < Columns > < asp:BoundField ReadOnly ="true" DataField ="DictCode" SortExpression ="DictCode" HeaderText ="字典编号" > < HeaderStyle HorizontalAlign ="Left" /> </ asp:BoundField > < asp:BoundField DataField ="DictName" SortExpression ="DictName" HeaderText ="字典名称" > < HeaderStyle HorizontalAlign ="Left" /> </ asp:BoundField > < asp:CheckBoxField DataField ="IfUsing" SortExpression ="IfUsing" HeaderText ="启用" Text ="启用" > < HeaderStyle HorizontalAlign ="Left" /> </ asp:CheckBoxField > < asp:BoundField DataField ="Note" HeaderText ="备注" > < HeaderStyle HorizontalAlign ="Left" /> </ asp:BoundField > < asp:CommandField CancelText ="取消" EditText ="编辑" ShowEditButton ="True" UpdateText ="更新" > </ asp:CommandField > < asp:CommandField DeleteText ="删除" ShowDeleteButton ="True" ></ asp:CommandField > </ Columns > < PagerStyle BackColor ="#666666" ForeColor ="White" HorizontalAlign ="Center" /> < SelectedRowStyle BackColor ="#C5BBAF" Font-Bold ="True" ForeColor ="#333333" /> < HeaderStyle BackColor ="#1C5E55" BorderStyle ="None" Font-Bold ="True" ForeColor ="White" HorizontalAlign ="Left" /> < EditRowStyle BackColor ="#7C6F57" /> < AlternatingRowStyle BackColor ="White" /> </ asp:GridView > < table style ="width: 100%" > < tr > < td > 字典编号: </ td > < td > < asp:TextBox ID ="txtDictCode" runat ="server" ></ asp:TextBox > </ td > </ tr > < tr > < td > 字典名称 </ td > < td > < asp:TextBox ID ="txtDictName" runat ="server" ></ asp:TextBox > </ td > </ tr > < tr > < td > 是否启用: </ td > < td > < asp:CheckBox ID ="chkIfUsing" runat ="server" Checked ="True" Text ="启用" /> </ td > </ tr > < tr > < td > 备注: </ td > < td > < asp:TextBox ID ="txtNote" runat ="server" Height ="88px" TextMode ="MultiLine" Width ="313px" ></ asp:TextBox > </ td > </ tr > < tr > < td colspan ="2" > < asp:Button ID ="btnSave" runat ="server" Text ="保存" OnClick ="btnSave_Click" /> < br /> < asp:Label ID ="lblInfo" runat ="server" /> < asp:Label ID ="lblSort" runat ="server" Visible ="false" ></ asp:Label > </ td > </ tr > </ table > </ div > </ form > </ body > </ html > 后台代码: using System; using System.Collections; 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.Linq; using System.Collections.Generic; namespace WebApplication1 ... { public partial class Default5 : System.Web.UI.Page ...{ /**//*作者:WhirlWind(20080524) */ ProviderTypeDataContext myprovider = new ProviderTypeDataContext(); protected void Page_Load(object sender, EventArgs e) ...{ if (!Page.IsPostBack) ...{ //进行绑定 gvBind(); } } // 进行数据绑定 public void gvBind() ...{ var query = from p in myprovider.Provider_Type select p; this.gvDictionary.DataSource = query; this.gvDictionary.DataBind(); } //添加操作 protected void btnSave_Click(object sender, EventArgs e) ...{ //判断当前主键编号是否存在 var bol = from p in myprovider.Provider_Type where p.DictCode.Contains(this.txtDictCode.Text) select p.DictCode; if (bol.Count() >= 1) ...{ this.lblInfo.ForeColor = System.Drawing.Color.Red; this.lblInfo.Text = string.Format("字典编号为 '{0}' 已经存在,请重新填写", this.txtDictCode.Text); return; } else ...{ this.lblInfo.Text = ""; } Provider_Type provider = new Provider_Type(); provider.DictCode = this.txtDictCode.Text; provider.DictName = this.txtDictName.Text; provider.IfUsing = this.chkIfUsing.Checked; provider.Note = this.txtNote.Text; //进行添加操作 //Table<Provider_Type> TabProviderTypes = myprovider.GetTable<Provider_Type>(); // myprovider.Provider_Type.Add(provider); // TabProviderTypes.res myprovider.Provider_Type.InsertOnSubmit(provider); myprovider.SubmitChanges(); //重新进行绑定 this.gvBind(); } protected void gvDictionary_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) ...{ this.gvDictionary.EditIndex = -1; this.gvBind(); } protected void gvDictionary_RowEditing(object sender, GridViewEditEventArgs e) ...{ this.gvDictionary.EditIndex = e.NewEditIndex; this.gvBind(); } //更新操作 protected void gvDictionary_RowUpdating(object sender, GridViewUpdateEventArgs e) ...{ //获取字典编号值 string strDictCode = this.gvDictionary.Rows[e.RowIndex].Cells[0].Text; Provider_Type provider = myprovider.Provider_Type.Single(p => p.DictCode == strDictCode); //更新内容 provider.DictName = ((TextBox)this.gvDictionary.Rows[e.RowIndex].Cells[1].Controls[0]).Text; provider.IfUsing = ((CheckBox)this.gvDictionary.Rows[e.RowIndex].Cells[2].Controls[0]).Checked; provider.Note = ((TextBox)this.gvDictionary.Rows[e.RowIndex].Cells[3].Controls[0]).Text; //进行更新操作 myprovider.SubmitChanges(); //取消更新状态 this.gvDictionary.EditIndex = -1; //重新进行绑定 this.gvBind(); } protected void gvDictionary_RowDeleting(object sender, GridViewDeleteEventArgs e) ...{ //获取主键 string strDictCode = this.gvDictionary.Rows[e.RowIndex].Cells[0].Text; var provider = from p in myprovider.Provider_Type where p.DictCode.Contains(strDictCode) select p; //执行删除操作 //myprovider.ProviderTypes.RemoveAll(provider); myprovider.Provider_Type.DeleteAllOnSubmit(provider); //开始提交 myprovider.SubmitChanges(); //重新进行绑定 this.gvBind(); } //排序 protected void gvDictionary_Sorting(object sender, GridViewSortEventArgs e) ...{ //Table<Model.ProviderType> provider = from p in myprovider.ProviderTypes // select p; //if (this.lblSort.Text == e.SortExpression) //{ // this.lblSort.Text = e.SortExpression + " DESC"; //} //else //{ // this.lblSort.Text = e.SortExpression; //} var mysort = from p in myprovider.Provider_Type orderby p.DictName descending select p; this.gvDictionary.DataSource = mysort; this.gvDictionary.DataBind(); } //分页 protected void gvDictionary_PageIndexChanging(object sender, GridViewPageEventArgs e) ...{ this.gvDictionary.PageIndex = e.NewPageIndex; this.gvBind(); } protected void gvDictionary_RowDataBound(object sender, GridViewRowEventArgs e) ...{ if (e.Row.RowType == DataControlRowType.DataRow) ...{ string strMsg = "javascript:return confirm('您确定要删除 字典名称为:" + e.Row.Cells[1].Text + " 的信息吗?')"; e.Row.Cells[5].Attributes.Add("onclick", strMsg); } } }} 参考了网上别人的文章,并修改调试通过!