web:
<asp:gridview id="item" runat="server" width="100%" autogeneratecolumns="False" cellpadding="0" gridlines="None" allowpaging="True" pagesize="20" DataKeyNames="itemno" HorizontalAlign="Center">
<columns>
<asp:boundfield datafield="itemno" headertext="物品编号" ReadOnly="True">
<ItemStyle Width="50px" />
</asp:boundfield>
<asp:boundfield datafield="itemname" headertext="物品名称" >
<ItemStyle Width="60px" />
</asp:boundfield>
<asp:boundfield datafield="itemprice" headertext="物品价格(元)" >
<ItemStyle Width="60px" />
</asp:boundfield>
<asp:commandfield showdeletebutton="True" showeditbutton="True" headertext="操作" >
<ItemStyle Width="80px" />
</asp:commandfield>
</columns>
<footerstyle backcolor="#507CD1" font-bold="True" forecolor="White" />
<rowstyle backcolor="#EFF3FB" HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" Wrap="False" />
<editrowstyle backcolor="#2461BF" />
<selectedrowstyle backcolor="#D1DDF1" font-bold="True" forecolor="#333333" />
<pagerstyle backcolor="#2461BF" forecolor="#00C0C0" horizontalalign="Center" />
<headerstyle cssclass="header" height="30px" forecolor="White" />
<alternatingrowstyle backcolor="White" />
<emptydatarowstyle cssclass="header" forecolor="White" height="30px" />
</asp:gridview>dal: /// <summary>
/// 宿舍物品管理 在girdview中显示数据
/// </summary>
/// <returns></returns>
public static IList<Items> SelectAllItems()
{
IList<Items> list = new List<Items>();
string sql = "select * from Items";
using (SqlDataReader reader = DBHelper.ExcuteReader(sql))
{
if (reader.Read())
{
Items items = new Items();
items.ItemNo = reader["ItemNo"].ToString();
items.ItemName = reader["ItemName"].ToString();
items.ItemPrice = Convert.ToInt32(reader["ItemPrice"]);
list.Add(items);
}
return list;
}
}
/// <summary>
/// 更新宿舍物品信息 对应girdview的editbutton
/// </summary>
/// <param name="items">宿舍物品</param>
/// <returns></returns>
public static int UpdateItems(Items items)
{
int result = 0;
string sql = null;
StringBuilder builder = new StringBuilder();
builder.Append("update Items set");
builder.Append("ItemName = '" + items.ItemName + "',");
builder.Append("ItemPrice ='" + items.ItemPrice + "',");
builder.Append("ItemNo ='" + items.ItemNo + "',");
builder.Append("where Id ='" + items.Id + "')select @@ROWCOUNT");
sql = builder.ToString();
try
{
result = DBHelper.ExcuteCommand(sql);
}
catch (Exception e)
{
throw e.InnerException;
}
finally
{
DBHelper.CloseConnction();
}
return result;
}
/// <summary>
/// 根据Id删除信息 对应girdview的deletebutton
/// </summary>
/// <param name="Id">物品编号</param>
/// <returns></returns>
public static int DeleteItems(int id)
{
int result = 0;
string sql = "delete from Items where Id = " + " " + id + " ";
try
{
result = DBHelper.ExcuteCommand(sql);
}
catch (Exception e)
{
throw e.InnerException;
}
finally
{
DBHelper.CloseConnction();
}
return result;
}
bll: public static int UpdateItems(Items items)
{
return ItemsDAL.UpdateItems(items);
}
public static int DeleteItems(int id)
{
return ItemsDAL.DeleteItems(id);
}
public static IList<Items> SelectALLItems()
{
return ItemsDAL.SelectAllItems();
}
<asp:gridview id="item" runat="server" width="100%" autogeneratecolumns="False" cellpadding="0" gridlines="None" allowpaging="True" pagesize="20" DataKeyNames="itemno" HorizontalAlign="Center">
<columns>
<asp:boundfield datafield="itemno" headertext="物品编号" ReadOnly="True">
<ItemStyle Width="50px" />
</asp:boundfield>
<asp:boundfield datafield="itemname" headertext="物品名称" >
<ItemStyle Width="60px" />
</asp:boundfield>
<asp:boundfield datafield="itemprice" headertext="物品价格(元)" >
<ItemStyle Width="60px" />
</asp:boundfield>
<asp:commandfield showdeletebutton="True" showeditbutton="True" headertext="操作" >
<ItemStyle Width="80px" />
</asp:commandfield>
</columns>
<footerstyle backcolor="#507CD1" font-bold="True" forecolor="White" />
<rowstyle backcolor="#EFF3FB" HorizontalAlign="Center" VerticalAlign="Middle" Width="100px" Wrap="False" />
<editrowstyle backcolor="#2461BF" />
<selectedrowstyle backcolor="#D1DDF1" font-bold="True" forecolor="#333333" />
<pagerstyle backcolor="#2461BF" forecolor="#00C0C0" horizontalalign="Center" />
<headerstyle cssclass="header" height="30px" forecolor="White" />
<alternatingrowstyle backcolor="White" />
<emptydatarowstyle cssclass="header" forecolor="White" height="30px" />
</asp:gridview>dal: /// <summary>
/// 宿舍物品管理 在girdview中显示数据
/// </summary>
/// <returns></returns>
public static IList<Items> SelectAllItems()
{
IList<Items> list = new List<Items>();
string sql = "select * from Items";
using (SqlDataReader reader = DBHelper.ExcuteReader(sql))
{
if (reader.Read())
{
Items items = new Items();
items.ItemNo = reader["ItemNo"].ToString();
items.ItemName = reader["ItemName"].ToString();
items.ItemPrice = Convert.ToInt32(reader["ItemPrice"]);
list.Add(items);
}
return list;
}
}
/// <summary>
/// 更新宿舍物品信息 对应girdview的editbutton
/// </summary>
/// <param name="items">宿舍物品</param>
/// <returns></returns>
public static int UpdateItems(Items items)
{
int result = 0;
string sql = null;
StringBuilder builder = new StringBuilder();
builder.Append("update Items set");
builder.Append("ItemName = '" + items.ItemName + "',");
builder.Append("ItemPrice ='" + items.ItemPrice + "',");
builder.Append("ItemNo ='" + items.ItemNo + "',");
builder.Append("where Id ='" + items.Id + "')select @@ROWCOUNT");
sql = builder.ToString();
try
{
result = DBHelper.ExcuteCommand(sql);
}
catch (Exception e)
{
throw e.InnerException;
}
finally
{
DBHelper.CloseConnction();
}
return result;
}
/// <summary>
/// 根据Id删除信息 对应girdview的deletebutton
/// </summary>
/// <param name="Id">物品编号</param>
/// <returns></returns>
public static int DeleteItems(int id)
{
int result = 0;
string sql = "delete from Items where Id = " + " " + id + " ";
try
{
result = DBHelper.ExcuteCommand(sql);
}
catch (Exception e)
{
throw e.InnerException;
}
finally
{
DBHelper.CloseConnction();
}
return result;
}
bll: public static int UpdateItems(Items items)
{
return ItemsDAL.UpdateItems(items);
}
public static int DeleteItems(int id)
{
return ItemsDAL.DeleteItems(id);
}
public static IList<Items> SelectALLItems()
{
return ItemsDAL.SelectAllItems();
}
解决方案 »
- 如何远程获取html源码,不从缓存中读取?
- WinForm下显示picturebox的说明?
- 如何在winform中怎么引用Excel9.0 lib,把DataGird中的数据用循环赋值到生成的文件Excel中的单元格,在线等待结贴!,(附参考代码)
- C#能写Video For Window的东东吗?
- 求教C#关于操作mysql的一些语法问题
- 关于ArcGIS10.1加入卫星影像图时,先后绘制的图片色彩不一致
- vs2010,office2007,C#,winform,在程序窗体中打开excel文件 [问题点数:100分]
- 如何生成考题
- 如何加快Winform的装载速度?
- 请问c#里如何让excel表格里的数据显示到textbox控件中
- TextBox有下拉条吗
- 批量处理字符串
=====
string sql = "delete from Items where Id = " + id ;
item.DataSource=bll.SelectALLItems();
item.DataBind();
{
int id = Convert.ToInt32(Request.QueryString["Id"]);
int itemNo = Convert.ToInt32(Request.QueryString["ItemNo"]);
if (!Page.IsPostBack)
{
this.DataBindItems();
if (id != 0)
{
int result = ItemsBLL.DeleteItems(id);
if (result > 0)
{
DataBindItems();
}
else
{
Response.Write("<script>alter('删除失败!')</script>");
}
}
if (itemNo != 0)
{
Items items = new Items();
items.ItemNo = itemNo.ToString();
int result = ItemsBLL.UpdateItems(items);
if (result > 0)
{
DataBindItems();
}
else
{
Response.Write("<script>alter('编辑失败!')</script>");
}
}
}
}
public void DataBindItems()
{
this.item.DataSource = ItemsBLL.SelectALLItems();
this.item.DataBind(); }
protected void item_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
item.PageIndex = e.NewPageIndex;
this.DataBindItems();
}
这是我自己写的,有问题的,绑定数据的没问题,但是添加了之后不能及时显示,而至于我刚说的修改和删除功能就没有了,谁教教我怎么写啊!!!
gridview自带了编辑和删除
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Del"))
{
UnitBLL.Delete(int.Parse(e.CommandArgument.ToString()));
BindData();
}
}
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
this.gv.EditIndex = e.RowIndex;
Unit p = new Unit();
UnitBLL.Edit(p);
this.gv.EditIndex = -1;
BindData();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string sql = "";
sql = @"SELECT contract.amount,
contract.re,
contract.contents,
contract.isApproved,
contract.enddingTime,
contract.beginningTime,
contract.customerID,
contract.contractID,
customer.customerName
FROM contract
LEFT OUTER JOIN customer
ON contract.customerID = customer.customerID"; ViewState["sql_select"] = sql;
//this.Lable_count.Text = "共显示" + this.gv_contract.Rows.Count.ToString() + "条记录";
}
if (ViewState["sql_select"] != null)
{
this.sqlds_contract.SelectCommand = ViewState["sql_select"].ToString();
} }
protected void gv_contract_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "MyModify")
{
int index = Convert.ToInt32(e.CommandArgument);
string contractID = this.gv_contract.DataKeys[index]["contractID"].ToString();
this.objds_contract.SelectParameters["contractID"].DefaultValue = contractID;
this.fv_contract.ChangeMode(FormViewMode.Edit);
this.MultiView1.ActiveViewIndex = 1;
}
if (e.CommandName == "MyDetails")
{
int index = Convert.ToInt32(e.CommandArgument);
string contractID = this.gv_contract.DataKeys[index]["contractID"].ToString();
this.objds_contract.SelectParameters["contractID"].DefaultValue = contractID;
this.fv_contract.ChangeMode(FormViewMode.ReadOnly);
this.MultiView1.ActiveViewIndex = 1;
}
if (e.CommandName == "MyOrder")
{
int index = Convert.ToInt32(e.CommandArgument);
string contractID = this.gv_contract.DataKeys[index]["contractID"].ToString();
this.sqlds_order.SelectCommand = @"SELECT [order].productID,
[order].contractID,
[order].orderID,
product.productName,
productType.productTypeName
FROM [order]
LEFT OUTER JOIN contract
ON [order].contractID = contract.contractID
LEFT OUTER JOIN product
ON [order].productID = product.productID
LEFT OUTER JOIN productType
ON product.productTypeID = productType.productTypeID
WHERE contract.contractID='" + contractID + "'";
this.MultiView1.ActiveViewIndex = 3;
this.Label_order.Text = "共显示0条记录";
}
if (e.CommandName == "MyDelete")
{
int index = Convert.ToInt32(e.CommandArgument);
string contractID = this.gv_contract.DataKeys[index]["contractID"].ToString();
contract BLL = new contract();
try
{
BLL.deletecontract(contractID);
}
catch (Exception ex)
{
commTools.alert(this, ex.InnerException.Message);
}
commTools.alert(this, "删除成功!");
string sql = ViewState["sql_select"].ToString();
this.sqlds_contract.SelectCommand = sql;
this.gv_contract.DataBind();
//this.Lable_count.Text = "共显示" + this.gv_contract.Rows.Count.ToString() + "条记录";
}
} protected void fv_contract_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
if (e.Exception != null)
{ commTools.alert(this, e.Exception.InnerException.Message);
e.ExceptionHandled = true;
// this.MultiView1.ActiveViewIndex = 1;
}
}
protected void fv_contract_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
foreach (DictionaryEntry entry in e.NewValues)
{
if (string.IsNullOrEmpty(entry.Value as string))
{
e.NewValues[entry.Key] = null;
}
} }
protected void fv_contract_ItemInserting(object sender, FormViewInsertEventArgs e)
{
foreach (DictionaryEntry entry in e.Values)
{
if (string.IsNullOrEmpty(entry.Value as string))
{
e.Values[entry.Key] = null;
}
}
}protected void ib_AddRecord_Click(object sender, ImageClickEventArgs e)
{
this.fv_contract.ChangeMode(FormViewMode.Insert);
this.MultiView1.ActiveViewIndex = 1;
}protected void ib_Back_Insert_Click(object sender, ImageClickEventArgs e)
{
string sql = "";
sql = ViewState["sql_select"].ToString();
this.sqlds_contract.SelectCommand = sql;
this.MultiView1.ActiveViewIndex = 0;
this.gv_contract.DataBind();
}
protected void ib_Save_Insert_Click(object sender, ImageClickEventArgs e)
{
TextBox tb = (TextBox)this.fv_contract.FindControl("contractIDTextBox");
ViewState["contractID"] = tb.Text.ToString();
this.gv_contract.DataBind();
this.MultiView1.ActiveViewIndex = 2;
this.contractIDLabel.Text = ViewState["contractID"].ToString();
}凑合看看吧~我不知道对你有没有用
这是我以前用2008做的一个界面