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

推荐订阅源

Martin Fowler
Martin Fowler
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
M
MIT News - Artificial intelligence
F
Fortinet All Blogs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
N
Netflix TechBlog - Medium
G
Google Developers Blog
L
LangChain Blog
腾讯CDC
大猫的无限游戏
大猫的无限游戏
U
Unit 42
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
小众软件
小众软件
The GitHub Blog
The GitHub Blog
博客园_首页
GbyAI
GbyAI

博客园 - 花****半

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 ('删除吗')");

            }

        }

    }

}