Datarepeater删除仅删除顶部记录

发布于 2024-11-04 05:42:16 字数 1239 浏览 5 评论 0原文

我有一个数据中继器,以下代码仅删除第一条记录,无论选择哪一条。我并不完全相信这是使用数据中继器执行此操作的正确方法,但我找不到更好的解决方案。我需要能够选择任何记录并将其删除。

    //delete document
    private void cmdDeleteDoc_Click(object sender, EventArgs e)
    {

        if (this.dataRepeater1.CurrentItemIndex == 0)
        {

            //begin reset
            this.dataRepeater1.BeginResetItemTemplate();
            // Delete Row Here

            DataClasses1DataContext db = new DataClasses1DataContext();

            System.Data.DataRowView SelectedRowView;
            newCityCollectionDataSet.DocumentsRow SelectedRow;

            SelectedRowView = (System.Data.DataRowView)documentsBindingSource.Current;
            SelectedRow = (newCityCollectionDataSet.DocumentsRow)SelectedRowView.Row;


            var matchedDocument = (from c in db.GetTable<Document>()
                                   where c.DocIDKey == SelectedRow.DocIDKey
                                   select c).SingleOrDefault();

            db.Documents.DeleteOnSubmit(matchedDocument);
            db.SubmitChanges();

            LoadCaseNumberKey(matchedDocument.CaseNumberKey, false, "documents");
            this.dataRepeater1.EndResetItemTemplate();
        }


    }

任何帮助都会很棒!

I have a datarepeater and the following code ONLY deletes the FIRST record regardless of which one is selected. I am not entirely convinced this is the correct way to do it with a datarepeater but I could not find a better solution. I need to be able to select any record and delete it.

    //delete document
    private void cmdDeleteDoc_Click(object sender, EventArgs e)
    {

        if (this.dataRepeater1.CurrentItemIndex == 0)
        {

            //begin reset
            this.dataRepeater1.BeginResetItemTemplate();
            // Delete Row Here

            DataClasses1DataContext db = new DataClasses1DataContext();

            System.Data.DataRowView SelectedRowView;
            newCityCollectionDataSet.DocumentsRow SelectedRow;

            SelectedRowView = (System.Data.DataRowView)documentsBindingSource.Current;
            SelectedRow = (newCityCollectionDataSet.DocumentsRow)SelectedRowView.Row;


            var matchedDocument = (from c in db.GetTable<Document>()
                                   where c.DocIDKey == SelectedRow.DocIDKey
                                   select c).SingleOrDefault();

            db.Documents.DeleteOnSubmit(matchedDocument);
            db.SubmitChanges();

            LoadCaseNumberKey(matchedDocument.CaseNumberKey, false, "documents");
            this.dataRepeater1.EndResetItemTemplate();
        }


    }

Any help would be great!.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2024-11-11 05:42:16

我的猜测是,您的 documentsBindingSourcedataRepeater 混淆了。

您直观地“看到”的是 dataRepeater,而您“得到”的是 documentsBindingSource.Current(您将其检索为 SelectedRowView
它始终是设置为 0 索引。这是一个非常常见的 Winforms 控制陷阱。

My guess is that your are mixed up between your documentsBindingSource and your dataRepeater.

What you "see" visually is the dataRepeater, while what you "get", is the documentsBindingSource.Current (that you retrieve as being SelectedRowView)
which is always set to 0 index. This is an all-too-common Winforms control trap.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文