gridview数据操作(增删改查)的例子
请问哪位大哥有:对gridview数据操作(增删改查)的例子?最好是三层的,谢谢. --------------------编程问答-------------------- http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx看看吧 --------------------编程问答-------------------- http://kb.cnblogs.com/a/1549550/
网上资料,给你参考一下 --------------------编程问答-------------------- 写好增删改查的方法,GRIDVIEW 空间绑定好这些方法,点击启用增删改查就可以 --------------------编程问答-------------------- UI层
public void databind()//---------------------------------绑定房间经营表--------------------编程问答-------------------- BLL层
{
GridView1.DataSource = rmm.selectroommanage();
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)//-------------------------------------编辑按钮
{
GridView1.EditIndex = e.NewEditIndex;
bangding();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)//------------------------取消按钮
{
GridView1.EditIndex = -1;
bangding();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)//--------------------------更新事件
{
int days;
Int64 CSID;
//int days = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString());
if (!(Int32.TryParse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString(), out days)))
{
return;
}
if (!(Int64.TryParse(((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString(), out CSID)))
{
return;
}
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string price = GridView1.Rows[e.RowIndex].Cells[4].Text;
string name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();
//string csid = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString();
string csid = CSID.ToString();
string time = rmm.gettime();
string totalprice = (days * float.Parse(price)).ToString();
int fid = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text);
rm.Days = days;
rm.CSname = name;
rm.CSid = csid;
rm.Id = id;
rm.StartTime = time;
rh.RoomId = fid;
rh.CSid = csid;
rh.CSname = name;
rh.StartTime = time;
rh.TotalPrice = totalprice;
rmm.updateroommange(rm);
rhm.addroomhistory(rh);
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('请付款" + totalprice + "');navigate('roommanage.aspx')</script>");
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)//-------------------------------------------删除事件
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string csid = GridView1.Rows[e.RowIndex].Cells[7].Text;
string time1 = GridView1.Rows[e.RowIndex].Cells[8].Text;
string time = rmm.gettime();
rm.StartTime = time1;
rm.CSid = csid;
rm.Id = id;
rh.Id = rhm.findroomhistoryid(rm);
rh.EndTime = time;
if (rhm.updateroomhistoryendtime(rh))
{
if (rmm.deleteroommanage(rm))
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('退订成功!');navigate('roommanage.aspx')</script>");
}
}
}
//--------------------------------------------------------------------------------------------------------房间号绑定
public void roommanagedropdownlistfid()
{
using (SqlDataReader dr = rmm.GetDr("roommanagedropdownlistfid"))
{
while (dr.Read())
{
ListItem li = new ListItem(dr["fid"].ToString(), dr["mid"].ToString());
DropDownList1.Items.Add(li);
}
}
//using (SqlDataReader dr = rmm.GetDr("roommanagedropdownlistfid"))
//{
// List<x_roommange> list = new List<x_roommange>();
// while (dr.Read())
// {
// x_roommange rm = new x_roommange(dr["fid"].ToString(), dr["mid"].ToString());
// list.Add(rm);
// }
// DropDownList1.DataSource = list;
// DropDownList1.DataTextField = "fid";
// DropDownList1.DataValueField = "mid";
// DropDownList1.DataBind();
//}
}
//---------------------------------------------------------------------------------------------------------房间号联动
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
databind1();
}
public void databind1()
{
rm.Id = int.Parse(DropDownList1.SelectedValue);
GridView1.DataSource = rmm.roommanagedropdownlist1liandong(rm);
GridView1.DataBind();
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
databind2();
}
public void databind2()
{
if (DropDownList2.SelectedValue == "1")
{
GridView1.DataSource = rmm.roommanagestateweiding();
GridView1.DataBind();
}
if (DropDownList2.SelectedValue == "2")
{
GridView1.DataSource = rmm.roommanagestateyiding();
GridView1.DataBind();
}
}
protected void btnselect_Click(object sender, EventArgs e)
{
databind();
DropDownList1.SelectedValue = "0";
DropDownList2.SelectedValue = "0";
}
public void bangding()//----------------------------------------------------------------------------------------编辑特定行
{
if (DropDownList1.SelectedValue != "0")
{
databind1();
}
if (DropDownList1.SelectedValue == "0" && DropDownList2.SelectedValue == "0")
{
databind();
}
if (DropDownList2.SelectedValue != "0")
{
databind2();
}
if (DropDownList1.SelectedValue != "0" && DropDownList2.SelectedValue != "0")
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('房间号和状态只能选择一个');navigate('roommanage.aspx')</script>");
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
if (DropDownList2.SelectedValue != "0")
{
databind2();
}
else
{
databind();
}
}
namespace Hotel.BLL--------------------编程问答-------------------- 参考 --------------------编程问答-------------------- DAL层
{
public class x_roommangeManager
{
private x_roommangeDAO rmDAO = null;
public x_roommangeManager()
{
rmDAO = new x_roommangeDAO();
}
#region 取出房间操作表中所有信息,绑定房间经营表
/// <summary>
/// 取出房间操作表中所有信息,绑定房间经营表
/// </summary>
/// <returns></returns>
public DataTable selectroommanage()
{
return rmDAO.selectroommanage();
}
#endregion
#region 绑定房间号
/// <summary>
/// 绑定房间号
/// </summary>
/// <returns></returns>
public DataTable roommanagedropdownlistfid()
{
return rmDAO.roommanagedropdownlistfid();
}
#endregion
#region dropdownlist联动
/// <summary>
/// dropdownlist联动
/// </summary>
/// <param name="rm"></param>
/// <returns></returns>
public DataTable roommanagedropdownlist1liandong(x_roommange rm)
{
return rmDAO.roommanagedropdownlist1liandong(rm);
}
#endregion
#region 订房信息写入房间经营表中
/// <summary>
/// 订房信息写入房间经营表中
/// </summary>
/// <param name="rm"></param>
/// <returns></returns>
public bool updateroommange(x_roommange rm)
{
return rmDAO.updateroommange(rm);
}
#endregion
#region 退订房间,使房间管理表某个ID的记录恢复为原始状态,ID不删除
/// <summary>
/// 退订房间,使房间管理表某个ID的记录恢复为原始状态,ID不删除
/// </summary>
/// <param name="rm"></param>
/// <returns></returns>
public bool deleteroommanage(x_roommange rm)
{
return rmDAO.deleteroommanage(rm);
}
#endregion
#region 查询未定房间
/// <summary>
/// 查询未定房间
/// </summary>
/// <returns></returns>
public DataTable roommanagestateweiding()
{
return rmDAO.roommanagestateweiding();
}
#endregion
#region 查询已订房间
/// <summary>
/// 查询已订房间
/// </summary>
/// <returns></returns>
public DataTable roommanagestateyiding()
{
return rmDAO.roommanagestateyiding();
}
#endregion
#region 查询已订房间
/// <summary>
/// 获取客户端时间
/// </summary>
/// <returns></returns>
public string gettime()
{
return rmDAO.gettime();
}
#endregion
public SqlDataReader GetDr(string storename)
{
return rmDAO.GetDr(storename);
}
}
}
namespace Hotel.DAL
{
/// <summary>
/// 房间管理操作类
/// </summary>
public class x_roommangeDAO
{
private SQLHelper sqlhelper = null;
public x_roommangeDAO()
{
sqlhelper = new SQLHelper();
}
/// <summary>
/// 取出房间操作表中所有信息,绑定房间经营表
/// </summary>
/// <returns></returns>
public DataTable selectroommanage()
{
DataTable dt = new DataTable();
dt = sqlhelper.GetDt("selectroommanage", CommandType.StoredProcedure);
return dt;
}
/// <summary>
/// 绑定房间号
/// </summary>
/// <returns></returns>
public DataTable roommanagedropdownlistfid()
{
DataTable dt = new DataTable();
dt = sqlhelper.GetDt("roommanagedropdownlistfid", CommandType.StoredProcedure);
return dt;
}
/// <summary>
/// dropdownlist联动
/// </summary>
/// <param name="rm"></param>
/// <returns></returns>
public DataTable roommanagedropdownlist1liandong(x_roommange rm)
{
DataTable dt = new DataTable();
SqlParameter[] paras = new SqlParameter[]{
new SqlParameter("@mid",rm.Id)
};
dt = sqlhelper.GetDt("roommanagedropdownlist1liandong", paras, CommandType.StoredProcedure);
return dt;
}
/// <summary>
/// 订房信息写入房间经营表中
/// </summary>
/// <param name="rm"></param>
/// <returns></returns>
public bool updateroommange(x_roommange rm)
{
bool flag = false;
SqlParameter[] paras = new SqlParameter[]{
new SqlParameter("@days",rm.Days),
new SqlParameter("@name",rm.CSname),
new SqlParameter("@csid",rm.CSid),
new SqlParameter("@id",rm.Id),
new SqlParameter("@time",rm.StartTime)
};
int result = sqlhelper.ExecuteQuery("updateroommange", paras, CommandType.StoredProcedure);
if (result > 0)
{
flag = true;
}
return flag;
}
/// <summary>
/// 退订房间,使房间管理表某个ID的记录恢复为原始状态,ID不删除
/// </summary>
/// <param name="rm"></param>
/// <returns></returns>
public bool deleteroommanage(x_roommange rm)
{
bool flag = false;
SqlParameter[] paras = new SqlParameter[]{
new SqlParameter("@id",rm.Id)
};
int result = sqlhelper.ExecuteQuery("deleteroommanage", paras, CommandType.StoredProcedure);
if (result > 0)
{
flag = true;
}
return flag;
}
/// <summary>
/// 查询未定房间
/// </summary>
/// <returns></returns>
public DataTable roommanagestateweiding()
{
DataTable dt = new DataTable();
dt = sqlhelper.GetDt("roommanagestateweiding", CommandType.StoredProcedure);
return dt;
}
/// <summary>
/// 查询已订房间
/// </summary>
/// <returns></returns>
public DataTable roommanagestateyiding()
{
DataTable dt = new DataTable();
dt = sqlhelper.GetDt("roommanagestateyiding", CommandType.StoredProcedure);
return dt;
}
/// <summary>
/// 获取客户端时间
/// </summary>
/// <returns></returns>
public string gettime()
{
string time = string.Format("{0:yyyy-MM-dd H:mm:ss}", DateTime.Now);
return time;
}
public SqlDataReader GetDr(string storename)
{
return sqlhelper.GetDr(storename);
}
}
}
补充:.NET技术 , ASP.NET