惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
Jina AI
Jina AI
量子位
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
S
SegmentFault 最新的问题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
雷峰网
雷峰网
博客园 - 聂微东
美团技术团队
Last Week in AI
Last Week in AI
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园_首页
V
Visual Studio Blog
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog

博客园 - 花****半

lr三大组件关系图 C#-----属性和索引器 C#----继承 实验2 -DataList 增删改查等操作 开发实训 发生类型为 System.OutOfMemoryException 的异常。请问这是什么原因 进度条的问题 学习ajax-二级联动下拉列表 - 花****半 - 博客园 SharpZipLib 怎么提高压缩比? 2月14号 句话是什么意思?if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[PerPer SQL Server中的sysobjects” 是什么意思 在sysobjects表中,xtype=rf ASP好东西!所以 —共享 target 怎么把一个页面的数据传到另一个页面? 提示以下的错误信息:“未能在设计视图中打开,"" 块中,以不同方式将值括起来 ” 什么问题这是? split
实验1----GridView 增删改查
花****半 · 2009-03-25 · via 博客园 - 花****半

数据库

参考代码

using System;

using System.Data;

using System.Configuration;

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.Data.SqlClient;

public partial class _Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    { 

        if (!Page.IsPostBack)

        {

          ViewState["id"] = "id";

          ViewState["dir"] = "desc" ;

          Bind();

      }

    }

    /// <summary>

    /// 数据绑定

    /// </summary>

    public void Bind()

    {

        string strCon = "server=.;database=studentTable;pwd=;user id=sa";

        SqlConnection con = new SqlConnection(strCon);

        con.Open();

        string strSql = "select * from stuinfo";

        SqlDataAdapter da = new SqlDataAdapter(strSql,con);

        DataSet ds = new DataSet();

        da.Fill(ds);

        DataView dv = ds.Tables[0].DefaultView;

        dv.Sort = (string)ViewState["id"] + " " + (string)ViewState["dir"];

        this.GridView1.DataSource = dv;

        this.GridView1.DataBind();

        con.Close();

    }

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)

    {

    }

    /// <summary>

    /// 更新

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

    {

        string strName =((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();

        int nSex = Convert.ToInt32(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox1")).Text.ToString());

        string strSql = "update stuinfo set sex =" + nSex + ",name='" + strName + "'where id=" + this.GridView1.DataKeys[e.RowIndex].Value.ToString() + "";

        string strCon = "server=.;database=studentTable;pwd=;user id=sa";

        SqlConnection con = new SqlConnection(strCon);

        con.Open();

        SqlCommand com = new SqlCommand();

        com.Connection = con;

        com.CommandText = strSql;

        com.ExecuteNonQuery();

        con.Close();

        this.GridView1.EditIndex = -1;

        Bind();

    }

    /// <summary>

    /// 编辑

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

    {

        this.GridView1.EditIndex = e.NewEditIndex;

        this.Bind();

    }

    /// <summary>

    /// 删除

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

    {

        string strSql="delete from stuinfo where id="+this.GridView1.DataKeys[e.RowIndex].Value.ToString()+"";

        string strCon = "server=.;database=studentTable;pwd=;user id=sa";

        SqlConnection con = new SqlConnection(strCon);

        con.Open();

        SqlCommand com = new SqlCommand();

        com.Connection = con;

        com.CommandText = strSql;

        com.ExecuteNonQuery();

        con.Close();

        Bind();

    }

    /// <summary>

    /// 取消编辑

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

    {

        this.GridView1.EditIndex = -1;

        this.Bind();

    }

    /// <summary>

    /// 分页

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

        this.GridView1.PageIndex = e.NewPageIndex;

        this.Bind();

    }

    /// <summary>

    /// 排序

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)

    {

        string strDri = e.SortExpression;

        if (ViewState["id"].ToString() == strDri)

        {

            if (ViewState["dir"].ToString() == "desc")

            {

                ViewState["dir"] = "asc";

            }

            else

            {

                ViewState["dir"] = "desc";

            }

        }

        else

        {

            ViewState["id"]= e.SortExpression;

        }

        Bind();

    }

    /// <summary>

    /// 绑定

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

    {

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            ////e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='yellow'");

            //e.Row.Attributes.Add("onmouseover", " c=this.style.backgroundColor;this.style.backgroundColor='#223333'");

            //e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

            if (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal)

            {

                ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "return confirm ('删除吗')");

            }

        }

    }

}