0

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
korrowan
  • 563
  • 2
  • 15
  • 37
  • why this line `if (this.dataRepeater1.CurrentItemIndex == 0)`? – Mehdi LAMRANI May 02 '11 at 16:05
  • @Mika That line is not necessary you are correct. That was there because I copied it from another button in which there has to be a currentitem to function. Also I do not want this to fire if there is no record as well. – korrowan May 02 '11 at 16:17

1 Answers1

1

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.

Mehdi LAMRANI
  • 11,289
  • 14
  • 88
  • 130