GridView的一些使用小技巧

1. 模板页中的多选按钮操作

以下代码

数据库中字段为int

1:代表 true

0:代表false

在GridView中把它显示出来

(注:此处的ToolTip只是为了在后台能得到当前数据的ID,在选中之后直接进行修改)

<asp:CheckBox ID="CheckBox1" runat="server" ToolTip='<%# Eval("JY_ID") %>'  Checked='<%# (Eval("JY_IsUser").ToString() == "0") ? false: true %>' AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged" />

 

2.进行分页
  (注:必需重新绑定查询一次数据)
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;  //得用GridView的分页
        setData();
    }

 

3.对数据进行编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex; //得到要编辑的行
        setData();
    }

 

 4.取消更新操作
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        setData();
    }


5.  GridView绑定数据

    gvList.DataSource = ds;
            gvList.DataKeyNames = new string[] { "id" };   //加上这个id,以便在进行其它操作时得到
            gvList.DataBind();

   protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(gvList.DataKeys[e.RowIndex].Value.ToString());  //这个就是在删除操作中得到
    //int id =Convert.ToInt32(this.GridView1.Rows[e.RowIndex].Cells[0].Text); //这种方法也可以得到

            FriendLink.Delete(id);
            GetList();
        }


 6 .对数据进行更新(数据库)
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = Convert.ToInt32(this.GridView1.Rows[e.RowIndex].Cells[0].Text);      //得到当前要修改的ID
        Model.BM_FirstTitle model = bll.ExecuteQuery(id) as Model.BM_FirstTitle;         //得到要修改的数据的model
        model.BM_FirstName = ((TextBox)(this.GridView1.Rows[e.RowIndex].FindControl("textBox2"))).Text;  //model中要修改的数据

        bll.ExecuteUpdate(model);
        this.GridView1.EditIndex = -1;
        setData();
    }

 

 

 

 

posted @ 2009-08-18 22:40  天涯之巅  阅读(316)  评论(1编辑  收藏  举报