DataGrid编辑,删除主要代码

作者:互联网   出处:控件中国网   2014-11-05 19:05:57   阅读:1

#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();
        }

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat