GridView 增删改查

前端显示如下:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" 
            CellPadding="3" DataKeyNames="ID" GridLines="Horizontal" 
            onselectedindexchanging="GridView1_SelectedIndexChanging" Width="511px" 
            AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" 
            onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" 
            PageSize="3">
        <AlternatingRowStyle BackColor="#F7F7F7" />
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="编号" />
            <asp:BoundField DataField="StuName" HeaderText="姓名" />
            <asp:BoundField DataField="Phone" HeaderText="电话" />
            <asp:BoundField DataField="Address" HeaderText="地址" />
            <asp:BoundField DataField="City" HeaderText="城市" />
            <asp:CommandField SelectText="详细信息" ShowSelectButton="True" />
            <asp:CommandField ShowEditButton="True" />
        </Columns>
        <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
        <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
        <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
        <SortedAscendingCellStyle BackColor="#F4F4FD" />
        <SortedAscendingHeaderStyle BackColor="#5A4C9D" />
        <SortedDescendingCellStyle BackColor="#D8D8F0" />
        <SortedDescendingHeaderStyle BackColor="#3E3277" />
        </asp:GridView>

后台对应该的操作如下:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
             BindData();
        }
       
    }
    public void BindData()
    {
        StudentsBLL sb = new StudentsBLL();
        this.GridView1.DataSource = sb.BindDataGridView();
        this.GridView1.DataBind();
    }
    //选择
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string strId = this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
        Response.Redirect("Show.aspx?id="+strId);
    }
    //分页
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindData();
    }
    //编辑
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }
    //取消编辑
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        BindData();
    }
    //更新
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        StudentsBLL sb = new StudentsBLL();
        StudentsModel sm = new StudentsModel();
        sm.ID = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        sm.StuName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
        sm.Phone = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text;
        sm.Address = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text;
        sm.City = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text;
        if (sb.UpdateData(sm) > 0)
        {
            this.GridView1.EditIndex = -1;
            BindData();
        }
        else
        {
            Response.Write("更新失败!");
        }

    }
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值