문제

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!.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top