4.7.3. Showing the Invoice Lines
We have two choices for displaying the invoice lines:
Getting data for each invoice from the
INVOICE_LINE
navigation property and displaying the contents of this complex property in the detail grid, probably with LINQ transformationsGetting the data for each invoice with a separate LINQ query that will be re-executed when the cursor moves to another record in the master grid
Either way has its advantages and drawbacks.
The first one assumes that we want to retrieve all invoices at once for the specified period together with the bound data from the invoice lines when the invoice form is opened. Although it is done with one SQL query, it may take quite a while and requires a large amount of random-access memory. It is better suited to web applications where records are usually displayed page by page.
The second one is a bit more difficult to implement, but it allows the invoice form to be opened quickly and requires less resource. However, each time the cursor in the master grid moves, an SQL query will be executed, generating network traffic, albeit with only a small volume of data.
For our application we will use the second approach. We need an event handler for the BindingSource component for editing the current record:
private void masterBinding_CurrentChanged(object sender, EventArgs e) {
LoadInvoiceLineData(this.CurrentInvoice.Id);
detailGridView.DataSource = detailBinding;
}
Now, the method for loading the invoice data:
private void LoadInvoiceLineData(int? id) {
var dbContext = AppVariables.getDbContext();
var lines =
from line in dbContext.INVOICE_LINES
where line.INVOICE_ID == id
select new InvoiceLineView
{
Id = line.INVOICE_LINE_ID,
Invoice_Id = line.INVOICE_ID,
Product_Id = line.PRODUCT_ID,
Product = line.PRODUCT.NAME,
Quantity = line.QUANTITY,
Price = line.SALE_PRICE,
Total = Math.Round(line.QUANTITY * line.SALE_PRICE, 2)
};
detailBinding.DataSource = lines.ToBindingList();
}
We use the InvoiceLineView class as an extension:
public class InvoiceLineView {
public int Id { get; set; }
public int Invoice_Id { get; set; }
public int Product_Id { get; set; }
public string Product { get; set; }
public decimal Quantity { get; set; }
public decimal Price { get; set; }
public decimal Total { get; set; }
}
Unlike the |
Now we will add a special property for retrieving the current line of the document selected in the detail grid.
public InvoiceLineView CurrentInvoiceLine {
get {
return (InvoiceLineView)detailBinding.Current;
}
}