码迷,mamicode.com
首页 > Web开发 > 详细

asp.net-gridview

时间:2017-04-02 22:37:46      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:new   prot   localhost   绑定   tab   password   com   自己   text   

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bindgrid();
        }//编辑按键下的取消代码。将EditIndex=-1,然后在绑定数据库。

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
             string id = GridView1.DataKeys[e.RowIndex][0].ToString();
             DeleteGridView(id);
             Bindgrid();
        }// id = GridView1.DataKeys[e.RowIndex][0].ToString();获得girdwiew中表的主键,作为删除数据的标识。DeleteGirdView()为自己写的删除函数


        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Bindgrid();
        }//获取当前编辑状态

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex][0].ToString();
            string uid = ((TextBox )GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            string upassword = ((TextBox )GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            string upower = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            UpdataGridView(id, uid, upassword, upower);
            GridView1.EditIndex = -1;
            Bindgrid();

        }//获得GridView中选中行的各列数据,用UpdataGridView()更新数据

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Bindgrid();
        }//换页的时候获取当前编辑页
       

        public void UpdataGridView(string id,string uid,string upassword,string upower)
        {
           SqlConnection con=new SqlConnection ("server=localhost;database=YGXXXT;uid=sa;password=00");
           string sql="update Users set ID=‘"+id+"‘,UID=‘"+uid+"‘,UPassword=‘"+upassword+"‘,UPower=‘"+upower+"";
           SqlCommand cmd=new SqlCommand (sql,con );
           con.Open ();
           cmd.ExecuteNonQuery ();
           con.Close();
           
        }//更新数据库数据,注意sql语句里的‘"+id+‘"不要弄错格式~~~
        public void DeleteGridView(string id)
        {
            SqlConnection con = new SqlConnection("server=localhost;database=YGXXXT;uid=sa;password=00");
            string sql = "delete Users where ID=‘"+id +"";
            SqlCommand cmd = new SqlCommand(sql, con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }//删除数据库数据

 

asp.net-gridview

标签:new   prot   localhost   绑定   tab   password   com   自己   text   

原文地址:http://www.cnblogs.com/gnsylyj/p/6660008.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!