你所在位置:首页Asp.net开发 → DataGrid编辑,删除主要代码备用

DataGrid编辑,删除主要代码备用

发布时间:2019-07-11

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

 摘自 大蜗牛


上一篇:asp.net 上传图片时出现“对路径“D:....”的访问被拒绝
下一篇:关于资源文件