簡體   English   中英

如何在沒有數據庫查詢的情況下刪除gridview中的行

[英]how I can delete row in gridview without queries in DB

有我的gridview

<asp:GridView ID="Grid" runat="server" AutoGenerateColumns="false" AutoGenerateEditButton="true"
    ForeColor="#333333" Width="600px" OnRowEditing="Grid_RowEditing" OnRowDeleting="Grid_RowDeleting"
    OnRowUpdating="Grid_RowUpdating" OnRowDeleted="Grid_RowDeleted" OnRowCancelingEdit="Grid_RowCancelingEdit"
    ShowFooter="True" DataKeyNames="id">
    <Columns>
        <asp:CommandField ShowDeleteButton="true" />
        <asp:CommandField ShowInsertButton="true" ShowHeader="true" />
        <asp:BoundField ReadOnly="true" DataField="ProductID" HeaderText="ProductID" NullDisplayText="sad" />
        <asp:BoundField ReadOnly="true" DataField="ProductName" HeaderText="ProductName" />
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
        <asp:BoundField DataField="UnitPrice" HeaderText="TotalPrice" />
    </Columns>
</asp:GridView>

我想用這種方法刪除行

protected void Grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int index = Convert.ToInt32(e.RowIndex);
    Grid.DeleteRow(index);
}

但是幾秒鍾后

此網頁無法使用

我該怎么辦?

有我的數據源。 我想刪除行而不刪除數據庫中的記錄

var query = 
    from order in mydb.Order_Details
    join orderdetail in mydb.Order_Details on order.OrderID equals orderdetail.OrderID
    join product in mydb.Products on order.ProductID equals product.ProductID
    where order.OrderID==editOrderID
    select new
    {
        ProductID = order.Product.ProductID,
        ProductName = order.Product.ProductName,
        Quantity = order.Quantity,
        UnitPrice = order.UnitPrice
    }
    into f
    group f by f.ProductID;

var outputData = query.Select(g => new
    {
        id= g.Key,
        ProductID = g.FirstOrDefault().ProductID,
        ProductName = g.FirstOrDefault().ProductName,
        Quantity = g.FirstOrDefault().Quantity,
        UnitPrice = g.FirstOrDefault().UnitPrice
    });

Grid.DataSource = outputData;
Grid.DataBind();

可以從RowDeleting事件中使該行invisible ,而不是從UI中刪除-

GridView1.rows[i].visible=false;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM