首页 新闻 会员 周边

怎么动态给Gridview添加数据

0
悬赏园豆:20 [已解决问题] 解决于 2009-12-02 08:39
代码
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width
="185px">
<Columns>
<asp:BoundField HeaderText="产品名称" />
<asp:BoundField HeaderText="产品数量" />
<asp:BoundField HeaderText="操作" />
</Columns>
</asp:GridView>
 <table width="100%" border="0" cellspacing="0" cellpadding="0">
       
         <tr>
           <td width="11%"><asp:TextBox runat="server" name="NAME" ID="NAME" size="10" /></td>
           <td width="14%"><asp:TextBox runat="server" name="NUM"  ID="NUM" size="10"/></td>
           <td  width="75%">&nbsp;</td>
         </tr>
         <tr>
           <td>&nbsp;</td>
           <td>&nbsp;</td>
           <td><asp:Button runat="server" ID="addyearPro" Text="增加" onclick="addyearPro_Click" /></td>
         </tr>
       </table>
<asp:Button runat="server" ID="addyearPro" Text="提交" onclick="addyearPro_Click" />

 

就是这样,有个GridView,有2个文本框,和一个添加按钮,最后有个“提交”按钮。

当点击添加按钮就向GridView添加数据。

不用数据库,就这样临时的放在控件里做显示。

在GridView中有一列是删除,单击删除,能删除该列数据。

 

当点击提交按钮时,把GridView确定下来的数据,再保存到数据库。

 

问题补充: 1楼的方法确实可以, 但怎么删除呢?
YangBayker的主页 YangBayker | 初学一级 | 园豆:95
提问于:2009-12-01 14:35
< >
分享
最佳答案
0

 兄弟,增加了删除的功能.

動態生成List<string>,然後綁定到GridView1

 

 

看看這個,用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">
                        &nbsp;</td>
                </tr>
                <tr>
                    <td>
                        &nbsp;</td>
                    <td>
                        &nbsp;</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>

收获园豆:16
woody.wu | 老鸟四级 |园豆:3621 | 2009-12-01 15:22
增加删除的功能了
woody.wu | 园豆:3621 (老鸟四级) | 2009-12-01 22:09
谢谢啊,现在我对GridView控件有了一些认识了;
YangBayker | 园豆:95 (初学一级) | 2009-12-02 08:40
其他回答(2)
0

首先不提倡这么做,web服务器压力太大,数据传输量对网络要求也大。

实现:

把List<Class> 序列化以后保存到ViewState中,若是嫌ViewState太大,可以把ViewState保存到数据库中。

每次绑定前需要把数据库中查询到的List与ViewState中的List合成后,再进行绑定。

实体类中最好标志位判断是否是数据库中的值。

实体类要求可序列化。

收获园豆:4
JerryShi | 园豆:254 (菜鸟二级) | 2009-12-01 17:31
0

非常不合理的做法

jason2013 | 园豆:1998 (小虾三级) | 2012-10-19 16:32
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册