• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
萌子
记录程序路上的点点滴滴……
博客园    首页    新随笔    联系   管理    订阅  订阅

VS2005 Gridview操作

 
//编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        dataBind();
    }
    //取消
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        dataBind();
    }
    //更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = this.GridView1.Rows[e.RowIndex];
        TextBox tb = (TextBox)row.Cells[1].Controls[0];
        if (tb.Text == "")
        {
            com.ShowAlert("新闻类别不能为空!");
        }
        else
        {
            if (mtDs.UpdateNewsClass(tb.Text, Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString())) > 0)
            {
                //com.ShowAlert("更新成功!");
                this.GridView1.EditIndex = -1;
                dataBind();
            }
            else
            {
                com.ShowAlert("更新失败!");
            }
        }
    }
    //删除

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string strsql;
            //int i = this.GridView1.EditIndex;
            int id = (int)GridView1.DataKeys[e.RowIndex].Value;
            //Label1.Text = i.ToString();
            //string id = ((TextBox)(GridView1.Rows[i].Cells[0].Controls[0])).Text.ToString();

            strsql = string.Format("delete * from   bigclassname where bigclassnameid=" + id + "");
            if (DBFun.ExecuteUpdate(strsql))
            {
                Response.Write("<script>alert('删除成功!');</script>");
                Response.Redirect("news_class.aspx");
            }
        }

 
checkBox的全选:
if (btnSelect.Text == "全 选")
        {
            foreach (GridViewRow row in this.GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.FindControl("chkSelect");
                cb.Checked = true;
            }
            this.btnSelect.Text = "全 消";
        }
        else if (btnSelect.Text == "全 消")
        {
            foreach (GridViewRow row in this.GridView1.Rows)
            {
                CheckBox cb =(CheckBox)row.FindControl("chkSelect");
                cb.Checked = false;
            }
            this.btnSelect.Text = "全 选";
        }
 
批量删除:
int a = 0;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");
            if (cb.Checked)
            {
                int >                try
                {
                    if (mtDs.DeleteNewsClass(id) > 0)
                    {
                        a = 1;
                    }
                    else
                    {
                        a = 2;
                    }
                }
                catch
                {
                    a = 2;
                }
            }
        }
        if (a == 1)
        {
            com.ShowAlert("删除成功!");
        }
        if (a == 2)
        {
            com.ShowAlert("删除失败!");
            dataBind();
        }
posted @ 2009-04-02 13:47  萌子  阅读(267)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3