控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

DataGrid编辑,删除主要代码

作者:未知 出处:cnblogs 2012年03月05日 阅读:

#region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {    
            this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler

(this.DataGrid1_PageIndexChanged);
            this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler

(this.DataGrid1_SortCommand);
            this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler

(this.DataGrid1_DeleteCommand);
            this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler

(this.DataGrid1_EditCommand);
            this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler

(this.DataGrid1_CancelCommand);
            this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler

(this.DataGrid1_UpdateCommand);
        }
        #endregion


private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            if(DataGrid1.Items.Count == 1)
            {
                if(DataGrid1.CurrentPageIndex !=0)
                {
                    DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex-1;
                }
            }
            us.Userid=int.Parse(e.Item.Cells[0].Text);
            us.users_del();
            bind();
        }

        private void DataGrid1_SortCommand(object source,

System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
        {
            string str=e.SortExpression;
            if (ViewState["sort"].ToString() == str + " desc")
            {
                ViewState["sort"] = str+" asc";
            }
            else
            {
                ViewState["sort"] = str+" desc";
            }
            bind();
        }

        protected void Button1_Click(object sender, System.EventArgs e)
        {
            string str = TextBox1.Text;
            ViewState["chazao"] ="useradmin like '%"+str+"%'";
            bind();
        
        }

        private void DataGrid1_PageIndexChanged(object source,

System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            bind();
        }
        protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
        {
            DataSet ds=  us.users_sel();
            DataView  dv= ds.Tables[0].DefaultView;
            DataGrid1.EditItemIndex = e.Item.ItemIndex;
            DataGrid1.DataSource=dv;
            DataGrid1.DataBind();
        }
        protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
        {
            DataSet ds = us.users_sel();
            DataView dv = ds.Tables[0].DefaultView;
            DataGrid1.EditItemIndex = -1;
            DataGrid1.DataSource = dv;
            DataGrid1.DataBind();
        }
        protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
        {
            string useradmin = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
            string username = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
            string email = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
       
            SqlConnection connUpdate = new SqlConnection(ConfigurationManager.AppSettings["connstr"]);
            connUpdate.Open();
            String sql_edit = "UPDATE users " + "SET useradmin = '" + useradmin + "'," + "username = '" + username +

"'," + "useremail='"+email+"' WHERE userid = " + e.Item.Cells[0].Text;
            SqlCommand sqlCommandUpdate = new SqlCommand(sql_edit,connUpdate);
            sqlCommandUpdate.ExecuteNonQuery();
            connUpdate.Close();
            DataSet ds = us.users_sel();
            DataView dv = ds.Tables[0].DefaultView;
            DataGrid1.EditItemIndex = -1;
            DataGrid1.DataSource = dv;
            DataGrid1.DataBind();
        }

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统