Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在gridview中删除行而不在DB中查询_C#_Asp.net_Gridview - Fatal编程技术网

C# 如何在gridview中删除行而不在DB中查询

C# 如何在gridview中删除行而不在DB中查询,c#,asp.net,gridview,C#,Asp.net,Gridview,这是我的观点 <asp:GridView ID="Grid" runat="server" AutoGenerateColumns="false" AutoGenerateEditButton="true" ForeColor="#333333" Width="600px" OnRowEditing="Grid_RowEditing" OnRowDeleting="Grid_RowDeleting" OnRowUpdating="Grid_RowUpdating" OnRo

这是我的观点

<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>
但几秒钟后我就明白了

此网页不可用

我该怎么办

这是我的数据源。我想删除行而不删除数据库中的记录

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

您可以在RowDeleteing事件中使该行不可见,而不是从UI中删除-

GridView1.rows[i].visible=false;

我可以想象您会遇到stackoverflow,因为每次通过Grid.DeleteRowindex命令调用Grid_RowDeleting。您应该从数据源/数据表中删除数据。因此,在本例中,您的代码缺少这一部分,无法在Grid.DeleteRowindex之后给出有意义的完整答案;再次绑定GridView…检查它…。例如Grid.DataBind@普拉纳夫也一样result@Icepickle. 我添加代码。我知道在数据库中删除记录时如何删除行,但我只想删除网格中的行。正如我所知,我需要将gridview与datable连接起来,但我不知道如何才能做到,当网格由查询LINQSQL填充时,您可以从DataGrid中获取键,我认为它称为grid.keys[rowIndex],然后获取数据源,并从中删除元素。您可能必须首先使用ToList强制转换outputData,因为现在它充满了匿名类,然后重新绑定数据源。我现在真的没有时间详细讨论,但如果到目前为止没有人回答,我今晚可能会回答:
GridView1.rows[i].visible=false;