简体   繁体   English

从数据库中删除一行和 wpf 中的 gridview

[英]Deleting a row from database and gridview in wpf

I want to delete a row from gridview and database - I write some code but this code just delete the first row of my gridview.我想从 gridview 和数据库中删除一行 - 我写了一些代码,但这段代码只是删除了我的 gridview 的第一行。 Please help me.请帮我。 I used Entity Framework and wpf C#.我使用了实体框架和 wpf C#。

using (AccountingEntities cntx = new AccountingEntities())
{
   Producer item = this.grdProducers.SelectedItem as Producer;
   cntx.DeleteObject(cntx.Producers.First(x => x.ID == item.ID));
   cntx.SaveChanges();
   dataPager.Source = cntx.Producers.ToList();
}

Maybe you should simple try:也许你应该简单尝试:

cntx.DeleteObject(cntx.Producers.where(x => x.ID == item.ID));

// if you get my .where() code to return the entity's index you'll should be fine

This should invoke the appropiate lambda/linq.这应该调用适当的 lambda/linq。 Since you use " where " the expression is applied to every "Producer"-Entity x matching item.ID由于您使用“ where ”,因此该表达式适用于每个“Producer”-Entity x匹配item.ID

UPDATE:更新:

From MSDN:来自 MSDN:

Deletes the record at the specified index from the data source.从数据源中删除指定索引处的记录。

DeleteObject(int rowIndex)

Well this explains a lot.好吧,这解释了很多。 Because this means, that your simply passing in the wrong argument.因为这意味着,您只是传递了错误的论点。 You need to loop through the whole Grid with a foreach or for and delete each entity with deleteObject and check before whether the object's id matches item.ID.您需要使用 foreach 或 for 遍历整个 Grid 并使用 deleteObject 删除每个实体,并检查对象的 id 是否与 item.ID 匹配。

I am sure this would be easier by using Lambda/LINQ but I currently have no idea how this could be done otherwise.我确信使用 Lambda/LINQ 会更容易,但我目前不知道如何做到这一点。

I also found this quite interesting, you have to scroll down to "delete", the example is for a database, but still uses the grid as buffer so it should be the similiar problem.我也觉得这很有趣,你必须向下滚动到“删除”,这个例子是针对数据库的,但仍然使用网格作为缓冲区,所以应该是类似的问题。

http://www.asp.net-crawler.com/articles/LINQ/Insert-retrieve-update-delete-through-gridview-using-LINQ-to-SQL.aspx http://www.asp.net-crawler.com/articles/LINQ/Insert-retrieve-update-delete-through-gridview-using-LINQ-to-SQL.aspx

I find the solution: when i open the dialog for confirming the delete action, selected item changed.我找到了解决方案:当我打开确认删除操作的对话框时,所选项目已更改。 i should select the entityId before opening the dialog .在打开对话框之前,我应该 select entityId the below code show how to do this:下面的代码显示了如何做到这一点:

            int unitTypeId = (this.grdUnitTypes.SelectedItem as UnitType).ID;
            ConfirmWindowResult result = Helpers.ShowConfirm(this, SR.GlobalMessages.AreYouSureToDelete, SR.GlobalMessages.Warning);
            if (result == ConfirmWindowResult.Yes)
            {
                using (AccountingEntities cntx = new AccountingEntities())
                {
                    try
                    {
                        cntx.UnitTypes.DeleteObject(cntx.UnitTypes.First(x => x.ID == unitTypeId));
                        cntx.SaveChanges();
                        dataPager.Source = cntx.UnitTypes.ToList();
                        MessageBox.Show("Success");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error");
                    }
                    finally
                    {
                        cntx.Dispose();
                    }
                }
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM