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! Please help me. I used Entity Framework and wpf C#.
我想从gridview和数据库中删除一行 - 我写了一些代码,但这段代码只删除了gridview的第一行!请帮帮我。我使用了Entity Framework和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();
}
2 个解决方案
#1
0
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. the below code show how to do this:
我找到了解决方案:当我打开确认删除操作的对话框时,所选项目已更改。我应该在打开对话框之前选择entityId。以下代码显示了如何执行此操作:
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();
}
}
}
#2
0
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. Since you use "where
" the expression is applied to every "Producer"-Entity x
matching item.ID
这应该调用适当的lambda / linq。由于您使用“where”表达式应用于每个“Producer”-Entity x匹配item.ID
UPDATE:
From 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循环遍历整个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.
我也发现这很有趣,你必须向下滚动到“删除”,例子是数据库,但仍然使用网格作为缓冲区,所以它应该是类似的问题。
#1
0
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. the below code show how to do this:
我找到了解决方案:当我打开确认删除操作的对话框时,所选项目已更改。我应该在打开对话框之前选择entityId。以下代码显示了如何执行此操作:
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();
}
}
}
#2
0
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. Since you use "where
" the expression is applied to every "Producer"-Entity x
matching item.ID
这应该调用适当的lambda / linq。由于您使用“where”表达式应用于每个“Producer”-Entity x匹配item.ID
UPDATE:
From 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循环遍历整个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.
我也发现这很有趣,你必须向下滚动到“删除”,例子是数据库,但仍然使用网格作为缓冲区,所以它应该是类似的问题。