简体   繁体   English

Datarepeater delete 仅删除顶部记录

[英]Datarepeater delete only deleting top record

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.我需要能够 select 任何记录并将其删除。

    //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..任何帮助都会很棒..

My guess is that your are mixed up between your documentsBindingSource and your dataRepeater .我的猜测是您的documentsBindingSource绑定源和您的dataRepeater之间混淆了。

What you "see" visually is the dataRepeater, while what you "get", is the documentsBindingSource.Current (that you retrieve as being SelectedRowView )您在视觉上“看到”的是 dataRepeater,而您“得到”的是documentsBindingSource.Current (您检索为SelectedRowView
which is always set to 0 index.始终设置为 0 索引。 This is an all-too-common Winforms control trap.这是一个非常常见的 Winforms 控制陷阱。

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

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