将数据从一个gridview移动到另一个gridview以进行编辑和更新 [英] Move data from one gridview to another for edit and update

查看:71
本文介绍了将数据从一个gridview移动到另一个gridview以进行编辑和更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在开展一个学校项目,创建一个允许屠夫在线注册和销售肉类的门户网站。现在我创建了两个girdviews,一个用于我的默认目录(显示产品以允许屠夫在创建目录时选择他们的产品),另一个将从gridview1到gridview2获取项目。现在在gridview2中,我希望屠夫能够选择和编辑产品,然后将它们保存到自己的ButcherProducts表中,该表具有StoreID,ProductID,Price,Quantity。现在我可以从gridview1到gridview2获取项目,但我正在努力放置编辑功能,所以屠夫可以编辑和保存。这是我的示例代码。

Hi all,
I am working on a school project to create a web portal that allows butchers to sign up and sell their meat online. now I have created two girdviews, one that i will use for my default catalog(displays products to allow butchers to select their products when creating their catalog)and the other one that will get items from gridview1 to gridview2. now in gridview2 i want butchers to be able to select and edit products and then save them to their own ButcherProducts table which has StoreID, ProductID, Price, Quantity. now i can get items from gridview1 to gridview2 but i am struggling to put the edit functionality so butchers can edit and save. here is my sample code.

Select Category:  <asp:DropDownList runat="server" ID="category" AutoPostBack="true"

                    SelectMethod="GetCatalogCategories" AppendDataBoundItems="true"

                    DataValueField="CategoryID" DataTextField="CategoryName">
                    <asp:ListItem Value="" Text="-select-"/>
                </asp:DropDownList> </td>
            <td>  </td>
            <td> </td>
        </tr>
        <tr>
          
            <td> <asp:GridView ID="Catalog" runat="server" 

               

                AutoGenerateColumns="False" 

                ItemType="DataAccess.Product"

              

                AllowSorting="false"

                 DataKeyNames="ProductID"

                

              

                SelectMethod="GetCatalog">
   
        <Columns>
            <asp:BoundField DataField="ProductID" HeaderText="ProductID"

                SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="Product Name"

                SortExpression="ProductName" />
            <asp:BoundField DataField="Description" HeaderText="Description"

                SortExpression="Description" />
           
            <asp:BoundField DataField="CategoryID" HeaderText="CategoryID"

                SortExpression="CategoryID" />
         
            <asp:TemplateField HeaderText="Category">
             
             <ItemTemplate>
                    <%#:Item.Category.CategoryName %>
                </ItemTemplate>


            </asp:TemplateField>
         
               <asp:TemplateField HeaderText="Image">
                    <ItemTemplate>
                 <img src="..Checkout/images/<%#:Item.ImagePath%>"

                                                width="20" height="25" border="1" />
                         </ItemTemplate>
                  
                    </asp:TemplateField>
            <asp:TemplateField HeaderText="Select Products">
                
               <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
     
     
        </Columns>



C#代码落后:


C# code behind:

List<product> tempIDList = new List<product>();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private MeatbyMobileContext _db = new MeatbyMobileContext();
        public IQueryable<category> GetCatalogCategories()
        {

            return _db.Categories;

        }

        public IQueryable<product> GetCatalog([Control]int? category)
        {
            var query = _db.Products.Select(p => p);
            if (category.HasValue)
            {
                query = query.Where(p => p.CategoryID == category.Value);
            
            }
            return query;
        
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
           

        for (int z = 0; z < Catalog.Rows.Count; z++)
        {
           MeatbyMobileContext db = new MeatbyMobileContext();

           
            CheckBox chk =  (CheckBox)Catalog.Rows[z].FindControl("CheckBox1");
            if (chk.Checked)
            {

              int dKey = Convert.ToInt32( Catalog.DataKeys[z].Value);
              
                Product product = (from c in db.Products
                                   where c.ProductID.Equals(dKey)
                                select c).SingleOrDefault();

                 tempIDList.Add(product );
                 



        
            }
        
        }

        GridView2.DataSource = tempIDList;
        GridView2.DataBind();



现在我想知道如何在数据在gridview2之后从这里移动。


now i would like help on how do i move from here after the data is in gridview2.

推荐答案

你必须这样做

现在你在第一页gridview中显示的tempdata信息存储在会话中

session [dtlist] = tempIDList;



现在转到paqe加载的另一个页面或者你想要的地方然后转换这个会话然后将表存储在另一个值中



i正在页面加载



datatable dt =(datatable)session [dtlist];

现在



gridview.datasource = dt;

gridview.databind



试着让我知道
you have to do like this
now your information of tempdata that is shown in first page gridview is stored in session
session["dtlist"]=tempIDList;

now go to another page on paqe load or where ever you want then cast this session and then store table in another value for Example

i am doing on page load

datatable dt=(datatable)session["dtlist"];
now

gridview.datasource=dt;
gridview.databind

Try and let me know


这篇关于将数据从一个gridview移动到另一个gridview以进行编辑和更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆