只使用LINQ to SQL删除子项?

时间:2015-06-13 23:06:22

标签: c# linq datagridview

我想删除使用LINQ to SQL在datagridview中选择的项目的子项,但它不起作用。我不知道是否可以这样做。我不知道如何处理,请您查看我的代码并告诉我我做错了什么? 这是我的代码:

private void cmdSupprimer_Click(object sender, EventArgs e)
{
    try
    {
        var catAct = (from catA in tbCategories.Categories
                               where catA.CategorieID == (int)dgCategories.Rows[0].Cells[0].Value       
                               select catA).Single();

        for (int i = 0; i < catAct.Jeuxes.Count(); i++)
        {
            for (int j = 0; j < catAct.Jeuxes[i].Personnages.Count(); j++)
            {
                tbPersos.Personnages.DeleteOnSubmit(catAct.Jeuxes[i].Personnages[j]);
            }
            tbJeux.Jeuxes.DeleteOnSubmit(catAct.Jeuxes[i]);
        }
        tbCategories.Categories.DeleteOnSubmit(catAct);

        tbCategories.SubmitChanges();

        dgCategories.DataSource = null;
        dgCategories.DataSource = (from cats in tbCategories.Categories
                                   select new CategorieDisplay()
                                   {
                                       CategoryID = cats.CategorieID,
                                       CategoryNom = cats.CategorieNom
                                   }).ToList();

        MessageBox.Show("Les enfants de cette catégorie ont été supprimés");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
} 

1 个答案:

答案 0 :(得分:0)

使您的数据源成为一个名为data的变量。然后,您可以从数据中删除行,datagridview也将更新。

var original = new Season[] {Season.Fall, Season.Winter};

var selectedSeasons = new object[original.Length];
for (int i = 0; i < original.Length; i++)
{
       selectedSeasons[i] = original[i];
}

var works = SomeFunctionWithObjectParamsArgs(selectedSeasons);