Gridview 手动绑定增删改查和分页

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyWeb._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="gvMain" runat="server" AutoGenerateColumns="False"
DataKeyNames
="ID" onrowdeleting="gvMain_RowDeleting"
onrowupdating
="gvMain_RowUpdating"
onpageindexchanging
="gvMain_PageIndexChanging"
onrowcancelingedit
="gvMain_RowCancelingEdit"
onrowediting
="gvMain_RowEditing" CellPadding="4" ForeColor="#333333"
GridLines
="None" Height="198px" Width="638px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="id" HeaderText="编号" />
<asp:BoundField DataField="name" HeaderText="名称" />
<asp:BoundField DataField="psw" HeaderText="密码" />

<asp:CommandField ShowEditButton="True" HeaderText="编辑" />
<asp:CommandField ShowDeleteButton="True" HeaderText="删除" />

</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>

</div>
</form>
</body>
</html>
 private string connStr = "Server=127.0.0.1;Port=2001;User Id=SYSDBA;Password=123456;Database=WWW;Encoding=GBK;";

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.Bind();
}

}
//数据绑定的方法
public void Bind()
{
OscarConnection conn
= new OscarConnection(connStr);
conn.Open();
string sql="SELECT * FROM A";
OscarCommand cmd
= new OscarCommand(sql, conn);
OscarDataAdapter da
= new OscarDataAdapter();
da.SelectCommand
= cmd;

DataSet ds
= new DataSet();
da.Fill(ds);

DataTable user
= ds.Tables[0];
this.gvMain.DataSource = user;
this.gvMain.AllowPaging = true;
this.gvMain.PageSize = 5;
this.gvMain.DataBind();

}
//删除的方法
public void DeleteA(int id)
{
OscarConnection conn
= new OscarConnection(connStr);
conn.Open();
string sql="delete from A where id="+id;
OscarCommand cmd
= new OscarCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
this.Response.Write("<script>alert('删除成功!');location.href='Default.aspx';</script>");

}
else
{
this.Response.Write("<script>alert('删除失败!');</script>");

}
}
//删除数据
protected void gvMain_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id =Convert.ToInt32(gvMain.DataKeys[e.RowIndex].Value);
if (id != 0)
{
DeleteA(id);
}
}
//更新数据
protected void gvMain_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(((TextBox)gvMain.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
string name = ((TextBox)gvMain.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
string psw = ((TextBox)gvMain.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();


OscarConnection conn
= new OscarConnection(connStr);
conn.Open();
string sql = "UPDATE A SET NAME='" + name + "', PSW='" + psw + "' WHERE ID=" + id;
OscarCommand cmd
= new OscarCommand(sql, conn);
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
this.Response.Write("<script>alert('更新成功!');location.href='Default.aspx';</script>");

}
else
{
this.Response.Write("<script>alert('更新失败!');</script>");

}
}


//取消编辑
protected void gvMain_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvMain.EditIndex
= -1;
Bind();
}
//行编辑时
protected void gvMain_RowEditing(object sender, GridViewEditEventArgs e)
{
gvMain.EditIndex
= e.NewEditIndex;
Bind();
}
//分页重新绑定数据
protected void gvMain_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvMain.PageIndex
= e.NewPageIndex;
Bind();

}

转载于:https://www.cnblogs.com/htht530/archive/2011/08/30/2159309.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值