2015-02-09 87 views
1

刪除的GridView行沒有查詢有我的GridView我怎麼能在DB

<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(); 
+1

我會想象你得到一個計算器,因爲你的'Grid_RowDeleting '每次通過'Grid.DeleteRow(index)'命令都會被調用。你應該從你的DataSource/DataTable中刪除數據。所以在這種情況下,你的代碼的這一部分在Grid.DeleteRow(index)之後缺少給出有意義的答案 – Icepickle 2015-02-09 12:22:40

+0

;再次綁定你的GridView ...檢查它........例如Grid.DataBind(); – 2015-02-09 12:27:19

+0

@ Pranav-BitWiser結果相同 – user4523894 2015-02-09 12:28:26

回答

0

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

GridView1.rows[i].visible=false;