首页  ·  知识 ·  云计算
Linq对GridView增删改操作
striveman  http://blog.csdn.net/striveman/  综合  编辑:dezai  图片来源:网络
1.创建数据表:--=============================================================================/**//****** 对象:表 dbo.Provider_Type ******

1.创建数据表:
--=============================================================================
/**//****** 对象:表 dbo.Provider_Type ******/
/**//****** 说明:供应商类型表表 ******/
--=============================================================================
If exists (select * from sysobjects where id = object_id('dbo.Provider_Type'))
Drop Table dbo.Provider_Type
GO
Create Table dbo.Provider_Type(
    TypeCode varchar(4) Not Null Primary Key,    --类型代码
    TypeName varchar(50) Not Null,            --类型名称;
    IfUsing bit Not Null Default 1,            --启用;默认启用
    Note varchar(100) Null                --备注;
)
GO
-------------------------------------------------------------------------------
2. 创建Visual Studio 2008 对数据库连接的关联操作。
菜单"View "-"Server Explorer"-"Add Connection"创建对当前数据的关联操作.
3.菜单“WebSite”-"Add New Item"添加新项操作,添加扩展名为“.dbml”文件.
4.将Provider_Type表从“Server Explorer”拖到该文件中,可根据需求对部分配置进行修改。
5.页面HTML代码如下:(我也想把图片粘出来,可是我弄上去,痛苦)



            OnRowEditing="gvDictionary_RowEditing" OnRowUpdating="gvDictionary_RowUpdating"
        OnRowDeleting="gvDictionary_RowDeleting"
        onpageindexchanging="gvDictionary_PageIndexChanging"
        onrowdatabound="gvDictionary_RowDataBound" onsorting="gvDictionary_Sorting">
       
           
           
           
           
           
           

           
       

   

   


       
           
           
       
       
           
           
       
       
           
           
       
       
           
           
       
       
           
       
   

                字典编号:

               
           

                字典名称

               
           

                是否启用:

               
           

                备注:

               
           

               
               

               
               
           


以下是对应的后台代码:


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.Linq;

public partial class Admin_Dictionary : System.Web.UI.Page
...{
    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.ProviderTypes
                    select p;

        this.gvDictionary.DataSource = query;
        this.gvDictionary.DataBind();
    }
 
    //添加操作
    protected void btnSave_Click(object sender, EventArgs e)
    ...{
        //判断当前主键编号是否存在
        var bol = from p in myprovider.ProviderTypes
                  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 = "";
        }
       

        ProviderType provider = new ProviderType();
       
        provider.DictCode = this.txtDictCode.Text;
        provider.DictName = this.txtDictName.Text;
        provider.IfUsing = this.chkIfUsing.Checked;
        provider.Note = this.txtNote.Text;

        //进行添加操作
        myprovider.ProviderTypes.Add(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;
        ProviderType provider = myprovider.ProviderTypes.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.ProviderTypes
                      where p.DictCode.Contains(strDictCode)
                      select p;
        //执行删除操作
        myprovider.ProviderTypes.RemoveAll(provider);
        //开始提交
        myprovider.SubmitChanges();
        //重新进行绑定
        this.gvBind();

    }
   
    //排序
    protected void gvDictionary_Sorting(object sender, GridViewSortEventArgs e)
    ...{
        //Table 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.ProviderTypes
                     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();

        //下面注释的方法,可用,但当执行后,可以进行翻页,只是翻过之后,就不能反过来了
        //int intSkip = (this.gvDictionary.PageIndex + 1) * this.gvDictionary.PageSize;
        //var query = (from p in myprovider.ProviderTypes
        //            select p).Skip(intSkip).Take(this.gvDictionary.PageSize);
       
        //this.gvDictionary.DataSource = query;
        //this.gvDictionary.DataBind();
    }
    protected void gvDictionary_RowDataBound(object sender, GridViewRowEventArgs e)
    ...{
        if (e.Row.RowType == DataControlRowType.DataRow)
        ...{
            string strMsg = string.Format("javascript:return confirm('您确定要删除 字典名称为:"{0}"  的信息吗?')", e.Row.Cells[1].Text);
            e.Row.Cells[5].Attributes.Add("onclick", strMsg);
        }
    }
}

本文作者:striveman 来源:http://blog.csdn.net/striveman/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读