如何在没有查询的情况下删除gridview中的行

时间:2015-02-09 12:14:20

标签: c# asp.net gridview

有我的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);
}

但几秒后我得到了

  

此网页不可用

我应该怎么做?

有我的数据源。我想删除行而不删除db

中的记录
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 个答案:

答案 0 :(得分:0)

您可以在RowDeleting事件中创建该行invisible -

,而不是从UI中删除
GridView1.rows[i].visible=false;