GridView的使用-4

protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnCX_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = new tbStuInfoManager().GetStuBySname(TextBox2.Text);
        GridView1.DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sno = (GridView1.Rows[e.RowIndex].FindControl("LinkButton1") as LinkButton).CommandArgument;
        if (new tbStuInfoManager().DeleteStuInfo(sno))
        {
            Response.Write("<script>alert('删除成功')</script>");
            GridView1.DataSource = new tbStuInfoManager().GetStuBySname(TextBox2.Text);
            GridView1.DataBind();
        }
        else
            Response.Write("<script>alert('删除失败')</script>");
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.DataSource = new tbStuInfoManager().GetStuBySname(TextBox2.Text);
        GridView1.DataBind();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        tbStuInfo tbstu=new tbStuInfo ();
        tbstu.Sno = (GridView1.Rows[e.RowIndex].FindControl("LinkButton1") as LinkButton).CommandArgument;
        tbstu.Sname = (GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox).Text;
        tbstu.Sex = (GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text;
        tbstu.ZhuanYe = (GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text;
        tbstu.JiGuan = (GridView1.Rows[e.RowIndex].FindControl("TextBox4") as TextBox).Text;
        if (new tbStuInfoManager().ModifyStuInfo(tbstu))
        {
            Response.Write("<script>alert('修改成功')</script>");
            GridView1.EditIndex = -1;//退出编辑状态
            GridView1.DataSource = new tbStuInfoManager().GetStuBySname(TextBox2.Text);
            GridView1.DataBind();
        }
        else
            Response.Write("<script>alert('修改失败')</script>");
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;//退出编辑状态
        GridView1.DataSource = new tbStuInfoManager().GetStuBySname(TextBox2.Text);
        GridView1.DataBind();
    }
public bool AddStuInfo(tbStuInfo tbstu)
       {
           string sql = string.Format("insert into tbStuInfos(Sno,Sname,Sex,ZhuanYe,JiGuan)values('{0}','{1}','{2}','{3}','{4}')", tbstu.Sno, tbstu.Sname, tbstu.Sex, tbstu.ZhuanYe, tbstu.JiGuan);
           return DBHelper.ExecuteNonQuery(DBHelper.ConnectionString, CommandType.Text, sql)>0;
       }
       public List<tbStuInfo> GetStuBySname(string name)
       {
           string sql = string.Format("select * from tbStuInfos where Sname like '%{0}%'", name);
           SqlDataReader dr = DBHelper.ExecuteReader(DBHelper.ConnectionString, CommandType.Text, sql);
           List<tbStuInfo> list=new List<tbStuInfo> ();
           while (dr.Read())
           {
               tbStuInfo tbf = new tbStuInfo();
               tbf.Sno = Convert.ToString(dr["Sno"]);
               tbf.Sname = Convert.ToString(dr["Sname"]);
               tbf.Sex = Convert.ToString(dr["Sex"]);
               tbf.ZhuanYe = Convert.ToString(dr["ZhuanYe"]);
               tbf.JiGuan = Convert.ToString(dr["JiGuan"]);
               list.Add(tbf);
           }
           dr.Close();
           return list; 
       }
       public bool DeleteStuInfo(string sno)
       {
           string sql = string.Format("delete tbStuInfos where Sno='{0}'", sno);
           return DBHelper.ExecuteNonQuery(DBHelper.ConnectionString, CommandType.Text, sql) > 0;
       }
       public bool ModifyStuInfo(tbStuInfo tbs)
       {
           string sql = string.Format("update tbStuInfos set Sname='{0}',Sex='{1}',ZhuanYe='{2}',JiGuan='{3}' where Sno='{4}'", tbs.Sname, tbs.Sex, tbs.ZhuanYe, tbs.JiGuan, tbs.Sno);
           return DBHelper.ExecuteNonQuery(DBHelper.ConnectionString, CommandType.Text, sql) > 0;
       }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值