asp.net操作GridView添删改查的两种方法 及 光棒效果

     这部份小内容很想写下来了,因为是基础中的基础,但是近来用的比较少,又温习了一篇,发现有点陌生了,所以,还是写一下吧。

方法一:使用Gridview本身自带的事件处理,代码如下(注意:每次操作完都得重新绑定,且删除列要转为模板列): 

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ShowCategories();
            ShowProducts();
        }
    }

    private void ShowProducts()
    {
        DataTable dt = NorthWind.DBHelp.GetTable("SELECT Product.产品ID, Product.产品名称, Supply.公司名称, Supply.城市, Category.类别名称, Category.图片, Product.单位数量, Product.单价, Product.库存量, Product.中止 FROM Category INNER JOIN Product ON Category.类别ID = Product.类别ID INNER JOIN Supply ON Product.供应商ID = Supply.供应商ID where Product.类别ID="+int.Parse(this.DropDownList1.SelectedValue));
        this.GridView1.DataSource = dt;
        this.GridView1.DataKeyNames = new string[]{"产品ID"};//设置数据操作的主键列
        this.GridView1.DataBind();
    }

    private void ShowCategories()
    {
        DataTable dt = NorthWind.DBHelp.GetTable("select * from Category");
        this.DropDownList1.DataSource = dt;
        this.DropDownList1.DataTextField = "类别名称";
        this.DropDownList1.DataValueField = "类别ID";
        this.DropDownList1.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        ShowProducts();
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //将当前要编辑的行的索引告诉GridView
        this.GridView1.EditIndex = e.NewEditIndex;
        ShowProducts();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //更新数据库
        decimal price = decimal.Parse(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox1")).Text);
        int productid = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());//获取当前行的主键
        //更新到数据库
        NorthWind.DBHelp.ExecuteNoQuery("update Product set 单价="+price+" where 产品ID="+productid);
        //重新绑定
        this.GridView1.EditIndex = -1;
        ShowProducts();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        ShowProducts();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //删除数据
        int productid = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());//获取当前行的主键
        //更新到数据库
        NorthWind.DBHelp.ExecuteNoQuery("delete from Product where 产品ID=" + productid);
        //重新绑定
        this.GridView1.EditIndex = -1;
        ShowProducts();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //当我们对数据源中的每一行进行数据绑定时触发的事件
        if (e.Row.RowType == DataControlRowType.DataRow)//判断绑定时候该行的状态时数据行还是其他类型的模板
        {
            //添加客户端确认
            LinkButton lnkDelete = (LinkButton)e.Row.Cells[10].FindControl("lnkDelete");
            lnkDelete.Attributes.Add("onclick", "return confirm('你确认删除吗?')");
            //光棒效果
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#ff66cc';");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        }
    }
    protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
    {
        //获取全选的复选框
        CheckBox chkSelectAll = (CheckBox)sender;
        //获取每一行的复选框
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)this.GridView1.Rows[i].Cells[11].FindControl("chkSelect");
            //修改状态
            chkSelect.Checked = chkSelectAll.Checked;
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //告诉GridView当前显示的数据是第几页的数据
        this.GridView1.PageIndex= e.NewPageIndex;
        ShowProducts();
    }

 

posted @ 2013-08-26 22:36  酒沉吟  阅读(358)  评论(0编辑  收藏  举报