
看看这个,用STATIC变量来保存数据,提交的时候移除。
--CS
using System
using System.Data
using System.Configuration
using System.Collections
using System.Web
using System.Web.Security
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.WebControls.WebParts
using System.Web.UI.HtmlControls
using System.Collections.Generic
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["A"] = "AAA"
}
}
//通过sessionid能获取维一的客户段内容
protected void addyearPro_Click(object sender, EventArgs e)
{
string SessionID = Session.SessionID
string Name = NAME.Text
string Qty = NUM.Text
Product pc = new Product(Name, Qty)
List<Product>newPList = ManagerProcut.Find(SessionID)
if (newPList != null)
{
newPList.Add(pc)
}
else
{
newPList = new List<Product>()
newPList.Add(pc)
ManagerProcut.pc.Add(SessionID, newPList)
}
GridView1.DataSource = newPList
GridView1.DataBind()
}
//来临时保存客户端的内容
public static class ManagerProcut
{
public static Dictionary<string, List<Product>>pc = new Dictionary<string, List<Product>>()
public static List<Product>Find(string Key)
{
if (pc.ContainsKey(Key))
{
return pc[Key]
}
else
{
return null
}
}
public static bool Remove(string Key)
{
if (pc.ContainsKey(Key))
{
return pc.Remove(Key)
}
else
{
return false
}
}
}
public class Product
{
public Product(string ProductName,string Qty)
{
this._ProductName = ProductName
this._Qty = Qty
}
private string _ProductName
private string _Qty
public string ProductName
{
get { return _ProductName}
set { _ProductName = value}
}
public string Qty
{
get { return _Qty}
set { _Qty = value}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in GridView1.Rows)
{
Response.Write("<p>新记录:")
foreach (TableCell tc in gr.Cells)
{
Response.Write("<br/>")
Response.Write(tc.Text)
}
Response.Write("</p>")
}
string SessionID = Session.SessionID
ManagerProcut.pc.Remove(SessionID)//移除
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
String Sessionid = Session.SessionID
GridViewRow gr = GridView1.Rows[e.RowIndex]
string ProductName = gr.Cells[0].Text
List<Product>per = ManagerProcut.Find(Sessionid)
if (per.Count >0)
{
Product pdt = null
foreach (Product pr in per)
{
if (pr.ProductName == ProductName)
{
pdt = pr
break
}
}
if (pdt != null)
{
bool m = per.Remove(pdt)
if (m)
{
GridView1.DataSource = per
GridView1.DataBind()
}
}
}
}
_aspx
<!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 id="Head1" runat="server">
<title>未命名页面</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="185px" OnRowDeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField HeaderText="产品名称" DataField="PRoductName" />
<asp:BoundField HeaderText="产品数量" DataField="QTY" />
<asp:ButtonField HeaderText ='Delete' CommandName="DELETE" Text="Delete"/>
</Columns>
</asp:GridView>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="11%" style="height: 24px">
<asp:TextBox ID="NAME" runat="server" /></td>
<td width="14%" style="height: 24px">
<asp:TextBox ID="NUM" runat="server" /></td>
<td width="75%" style="height: 24px">
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="增加" OnClick="addyearPro_Click" /></td>
</tr>
</table>
<asp:Button ID="Button2" runat="server" Text="提交" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
1、点击添加,实例化一个DataGridView的行对象DataRow 然后把这个对象添加到DataGridView中,你的DataGridView要是可编辑状态,然后编辑数据,点保存循环读取DataGridView的行数据,存到实体类中,在通过后台SQL保存到数据库。
2、将datagridview于数据库中对应的表进行绑定,绑定完成之后直接在datagridview中进行添加就可以,自动在数据库中添加并保存。
3、创建一个表结构,每次新增的时候往表里面加一条记录,然后DataGridView再绑定表就可以了。保存的时候连接数据库+sql就完成了。
4、就是正常的添加就可以了啊。首先连接数据库 而后插入数据库的表。
5、gridview没有自带这种功能,只自带了,编辑删除(也可能是我不知道,我没用过直接可以添加一行新的)你可以模仿一个这样的功能,用js动态添加一行textbox,每个字段对应一个,你把数据添加后,点击保存,就ok了,当然你的动态添加的textbox要在gridview下面,样式要对齐,感觉上是gridview添加了一行。
你好,很高兴为你解答!第一步:生成一个和gridview相同列的datatable(暂且叫tableA),用于绑定gridview。
第二步:把gridview的数据从数据库中取出,放入datatable,注意,这是另外一个datatable(暂且叫tableB),gridview的数据都从这里取。不过datatable的行是gridview的列。第一步的那个datatable仅仅用于gridview绑定用,没有任何其他用途。
第三步:根据第二步的tableB的数据行,动态生成gridview的列。同时add控件。
第四步:给gridview生成的动态控件绑定自己的事件。
第五步:给事件添加更改tableB的代码。
第六步:把tableB放进session.
完毕。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)