GridView的增删改查的例子

46 篇文章 0 订阅
这样吧,我发一个GridView的增删改查的例子给你:
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OASys.BLL;
using OASys.Models;
using System.Collections.Generic;


public partial class _Default : System.Web.UI.Page
{
    public UserInfoManager info = new UserInfoManager();


    /// <summary>
    /// 窗体加载事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadInfo();
        }
    }


    /// <summary>
    /// 加载用户信息的方法
    /// </summary>
    private void LoadInfo()
    {
        List<UserInfo> users =new List<UserInfo>();
        foreach (UserInfo user in info.GetAllUserInfo())
        {
            if (user.Gender == 1)
            {
                user.DepartId.sex = "男";
            }
            else
            {
                user.DepartId.sex = "女";
            }
            users.Add(user);
        }
        gdvUserInfo.DataSource =users;
        gdvUserInfo.DataBind();
    }


    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvUserInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvUserInfo.PageIndex = e.NewPageIndex;
        LoadInfo();
    }


    /// <summary>
    /// 删除信息事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvUserInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id =gdvUserInfo.DataKeys[e.RowIndex].Value.ToString();
        if (info.DeleteUserInfoById(id) > 0)
        {
            this.lblMessage.Text = "删除成功!";
            LoadInfo();
        }
        else
        {
            this.lblMessage.Text = "删除失败!";
            LoadInfo();
        }
    }


    /// <summary>
    /// 编辑事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvUserInfo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gdvUserInfo.EditIndex = e.NewEditIndex;
        LoadInfo();
    }


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


    /// <summary>
    /// 更新事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvUserInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string userId = gdvUserInfo.DataKeys[e.RowIndex].Value.ToString();
        TextBox txtUserId = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtUserId");
        TextBox txtUserName = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtUserName");
        TextBox txtPassWord = (TextBox)gdvUserInfo.Rows[e.RowIndex].Cells[0].FindControl("txtPassWord");
        if (txtUserName != null && txtPassWord != null)
        {
            try
            {
                UserInfo user = info.GetUserInfoById(userId);
                user.UserName = txtUserName.Text.ToString();
                user.Password = txtPassWord.Text.ToString();
                if (info.ModifyUserInfo(user) > 0)
                {
                    this.lblMessage.Text = "修改成功!";
                    gdvUserInfo.EditIndex = -1;
                    LoadInfo();
                }
                else
                {
                    this.lblMessage.Text = "修改失败!";
                    gdvUserInfo.EditIndex = -1;
                    LoadInfo();
                }
            }
            catch (Exception)
            {
                
               this.lblMessage.Text = "修改失败!";
                    gdvUserInfo.EditIndex = -1;
                    LoadInfo();
            }
        }
    }


    /// <summary>
    /// 
    /// 光棒事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gdvUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 0; i < gdvUserInfo.Rows.Count; i++)
        {
            if (gdvUserInfo.Rows[i].RowType == DataControlRowType.DataRow)
            {
                gdvUserInfo.Rows[i].Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#9cf'");
                gdvUserInfo.Rows[i].Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值