我使用以下代码从列表中获取和项目(票证)并根据发票号码删除。但它会删除列表中的第一项。
private void btnTktDelete_Click(object sender, EventArgs e)
{
uOp = true;
if (MainListBox.SelectedIndex != -1)
{
Ticket tkt = (Ticket)MainListBox.SelectedItem;
List<Ticket> tlist = GetList<Ticket>(tklp);
Ticket TheTicket = tlist.Find(x => x.InvoiceNumber == tkt.InvoiceNumber);
List<Transaction> tranlist = GetList<Transaction>(trlp);
if (TheTicket != null)
{
Int32 number = TheTicket.InvoiceNumber;
tlist.RemoveAll(x=>x.InvoiceNumber == number);
tranlist.RemoveAll(x => x.InvoiceNumber == number);
SaveList(trlp, tranlist);
SaveList(tklp, tlist);
}
// MainListBox.DataSource = null;
// MainListBox.DataSource = GetList<Ticket>(tklp);
// dgvExport.DataSource = null;
// dgvExport.DataSource = GetList<Ticket>(tklp);
uOp = false;
}
}
答案 0 :(得分:0)
您的代码建议多张票可以使用相同的发票号。这对我来说并不直观 - 应该是独一无二的。获取列表的最简单方法是在字段中存储对它的引用。
List<Ticket> allTickets = ...
private void btnTktDelete_Click(object sender, EventArgs e)
{
Ticket ticketToRemove = (Ticket)MainListBox.SelectedItem; // need cast?
allTickets.Remove(ticketToRemove);
// save...
}