• .net2005中对asp.net中GridView的常用操作


    1、先绑定数据源
     private void fBind()
        {
            SYJ.sqlServer f = new SYJ.sqlServer(SYJ.common.gDbConnStr(""));
            string strF = "select * from tFunctionWeb";
            DataSet ds = f.sqlSelectDS(strF);
            ViewState["dsPage"] = ds;                    //这在后面的分页中用到;
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }
    2、分页事件
     protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            this.GridView1.DataSource = ViewState["dsPage"];
            this.GridView1.DataBind();
        }
    3、删除事件
     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SYJ.sqlServer f = new SYJ.sqlServer(SYJ.common.gDbConnStr(""));
            int fid = int.Parse(this.GridView1.DataKeys[e.RowIndex].Values[0].ToString());
            string strF = "delete  from tFunctionWeb where fid="+fid;
            //string strF2 = "delete from ";
            if (f.sqlInsertUpdateDelete(strF) > 0)
            {
                SYJ.common.msgBox(this, "删除成功!");
            }
            else
            {
                SYJ.common.msgBox(this, "对不起,删除失败!");
            }
            this.fBind();
        }
    4、删除确认框事件
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[e.Row.Cells.Count - 1].Attributes.Add("onclick", "return confirm('确定要永久删除该功能页面吗?');");
            }
        } 

        /// <summary>
        /// (编辑)取消事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            this.fBind();
        }

        /// <summary>
        /// 编辑事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            this.fBind();
        }

        /// <summary>
        /// (编辑)更新事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            SYJ.sqlServer update = new SYJ.sqlServer(SYJ.common.gDbConnStr(""));
            //   string RegName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("tbRegName")).Text.Trim();   /*需要插入模板列,比较麻烦*/
           
            TextBox tbRegName = (TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0];//通过找到编辑所在行的列数即可
            string RegName2 = tbRegName.Text;
            if (!string.IsNullOrEmpty(RegName2))
            {
                StringBuilder strsql = new StringBuilder("UPDATE ai_ObjComm SET RegName='")
                                       .Append(RegName2)
                                       .Append("'")
                                       .Append(" WHERE ID='")
                                       .Append(GridView1.DataKeys[e.RowIndex].Value)
                                       .Append("'");

                try
                {
                    update.sqlInsertUpdateDelete(strsql.ToString());
                    GridView1.EditIndex = -1;
                    fBind();
                    Response.Write("<script>alert('信息更新成功');</script>");
                }
                catch (Exception ex)
                {
                    string message = "修改车辆资料失败,详细信息是:"+ex.Message.Replace("\r\n", "");
                    Response.Write("<script>alert(message);</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('车牌号码不能为空,请重新输入');</script>");
            }
        }
        /// <summary>
        /// 我们可能要根据需要,对gridview中的数据进行特殊的显示,比如当某样商品库存为0时,要求gridview中以不同颜色进行显示,这时,可以按如下的方法进行:
       /// 首先,gridview提供了rowdatabound事件,该事件在gridview中每行被创建并且绑定到datasource控件后被触发,因此,我们可以利用该事件去检查库存是否为0,如果为0的话,将所在行的北京颜色设置为黄色,
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int isHaveLock = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "isHaveLock"));
                if (isHaveLock == 0)
                    e.Row.BackColor = Color.Yellow;
            }

        }

  • 相关阅读:
    android之Fragment(官网资料翻译)
    Java获取当前时间的年月日方法
    Android Fragment Base
    Java JNI初探
    PHP 初学之登录查询小case
    PHP 初学
    tomcat:run和tomcat7:run的区别,以及Apache Tomcat Maven Plugin 相关
    Intellij IDEA:maven的本地仓库问题
    Java 构造方法的执行过程(猜测)
    PHP wamp server问题
  • 原文地址:https://www.cnblogs.com/bbxie/p/567161.html
Copyright © 2020-2023  润新知