前端显示如下:
<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("更新失败!");
}
}
}