【问题标题】:How to load gridview from data table without replacing previous data如何从数据表加载gridview而不替换以前的数据
【发布时间】:2017-12-13 08:59:14
【问题描述】:

我有一个文本框 txtId 和一个网格视图,网格视图根据提供的 id 显示数据。这很好,但是每当我第二次提供 Id 时,网格都会加载新数据并且我之前的 id 数据从 gidview 中消失了.

我想要这样的东西:

每当我输入 Idsecond 时,以前的数据都消失了,我想要新的数据,如上图所示。

下面是我使用 eval 绑定网格的网格视图:

<asp:GridView ID="GVProduct" CssClass="table table-hover" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="None" AllowPaging="True">
        <AlternatingRowStyle BackColor="#CCCCCC" />
        <Columns>
              <asp:TemplateField HeaderText="Product Name">
                <ItemTemplate>
                    <asp:Label ID="lblProductName" runat="server" Text='<%#Eval("ItemName") %>' ></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText="CP">
                <ItemTemplate>
                    <asp:Label ID="lblCP" runat="server" Text='<%#Eval("CostPrice") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
 </asp:GridView>

这就是我在单击按钮时加载网格的方式:

   protected void btnAdd_Click(object sender, EventArgs e)
        {
            int itemIds = Convert.ToInt32(txtPId.Text);
            int qty = Convert.ToInt32(txtQty.Text);
            if (txtPId.Text!=String.Empty || txtQty.Text != String.Empty)
            { 
                DataTable dt = bllProduct.GetProductById(itemIds);
                if (dt.Rows.Count>0)
                {
                    GVProduct.DataSource = dt;
                    GVProduct.DataBind();
                }
            }
        }

【问题讨论】:

    标签: asp.net gridview webforms


    【解决方案1】:

    我认为GetProductById(itemIds) 只返回一条记录,请检查GetProductById 方法中的选择查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多