Application Application: College Parc Auto-Repair
A data grid view is a flexible control that makes it possible to display a collection of values. This application explores some of its features, including…
5 min read1,166 words
Introduction#
The data grid view serves as a versatile control for presenting sets of data. This project demonstrates several of its capabilities, such as exporting the stored values into a standard text file. Application: Starting the Development
- Start Microsoft Visual C#
- Create a new Windows Application named CollegeParkAutoRepair1
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type CollegeParkAutoRepair.cs and press Enter
- In the Menus & Toolbars section of the Toolbox, click MenuStrip and click the form
- Under the form, right-click menuStrip1 and click Insert Standard Items
- In the Menus & Toolbars section of the Toolbox, click StatusStrip and click the form
- Design the form as follows: Control Name Text Other Properties GroupBox Order Identification Label Customer Name: TextBox txtCustomerName Label Address TextBox txtAddress Label City: TextBox txtCity Label State: TextBox txtState Label ZIP Code: TextBox txtZIPCode TextAlign: Right Label Make / Model: TextBox txtMake TextBox txtModel Label Year: TextBox txtCarYear TextAlign: Right Label Problem Description: TextBox txtProblem Mutltiline: True GroupBox Parts Used DataGridView dgvPartsUsed Columns Header Text Name Width Part Name/Description colPartName 215 Unit Price colUnitPrice 80 Qty colQuantity 30 Sub-Total colSubTotal 60 GroupBox Jobs Performed DataGridView dgvJobsPerformed Columns Header Text Name Width Job Performed colJobPerformed 320 Cost colCost 60 GroupBox Repair Summary Label Total Parts: TextBox txtTotalParts 0.00 TextAlign: Right Label Tax Rate: TextBox txtTaxRate 5.75 TextAlign: Right Label % Label Total Labor: TextBox txtTotalLabor 0.00 TextAlign: Right Label Tax Amount: TextBox txtTaxAmount 0.00 TextAlign: Right Label RepairTotal: TextBox txtRepairTotal 0.00 TextAlign: Right Label Recommendations TextBox txtRecommendations Multiline: True OpenFileDialog dlgOpen DefaultExt: rpr Filter: Repair Orders (*.rpr)|*.rpr|All Files| Title: Open Existing Repair Order SaveFileDialog dlgSave DefaultExt: rpr Filter: Repair Orders (*.rpr)|*.rpr|All Files| Title: Save Current Repair Order PrintDocument docPrint PrintDialog dlgPrint Document: docPrint PageSetupDialog dlgPageSetup Document: docPrint PrintPreviewDialog dlgPrintPreview Document: docPrint
- On the form, click the data grid view in the Parts Used group
- In the properties window, click Events and double-click CellLeave
- Implement the event as follows: void CalculateTotal() { double taxRate = 0d, taxAmount = 0d; double totalParts = 0d, totalLabor = 0d, totalPartsAndLabor, repairTotal; foreach( DataGridViewRow record in dgvPartsUsed.Rows) { try { totalParts += double.Parse(record.Cells[3].EditedFormattedValue.ToString()); txtTotalParts.Text = totalParts.ToString("F"); } catch (FormatException) { } } foreach (DataGridViewRow record in dgvJobsPerformed.Rows) { try { totalLabor += double.Parse(record.Cells[1].EditedFormattedValue.ToString()); txtTotalLabor.Text = totalLabor.ToString("F"); } catch (FormatException) { } } try { taxRate = double.Parse(txtTaxRate.Text); } catch (FormatException) { MessageBox.Show("Invalid Tax Rate"); txtTaxRate.Text = "5.75"; txtTaxRate.Focus(); } totalPartsAndLabor = totalParts + totalLabor; taxAmount = totalPartsAndLabor * taxRate / 100; repairTotal = totalPartsAndLabor + taxAmount; txtTotalParts.Text = totalParts.ToString("F"); txtTotalLabor.Text = totalLabor.ToString("F"); txtTaxAmount.Text = taxAmount.ToString("F"); txtRepairTotal.Text = repairTotal.ToString("F"); } private void dgvPartsUsed_CellLeave(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 2) { double unitPrice = 0D; int quantity = 0; double subTotal = 0D; DataGridViewCell dgvcUnitPrice = dgvPartsUsed.Rows[e.RowIndex].Cells[1]; DataGridViewCell dgvcQuantity = dgvPartsUsed.Rows[e.RowIndex].Cells[e.ColumnIndex]; unitPrice = double.Parse(dgvcUnitPrice.EditedFormattedValue.ToString()); quantity = int.Parse(dgvcQuantity.EditedFormattedValue.ToString()); subTotal = unitPrice * quantity; dgvPartsUsed.Rows[e.RowIndex].Cells[3].Value = subTotal.ToString("F"); CalculateTotal(); } }
- Return to the form
- Click the Jobs Performed data grid view
- In the Events section of the properties window, double-click CellLeave
- Implement the event as follows: private void dgvJobsPerformed_CellLeave(object sender, DataGridViewCellEventArgs e) { CalculateTotal(); }
- Return to the form
- On the form, click File and double-click New Repair Order
- Implement its Click event as follows: private void newToolStripMenuItem_Click(object sender, EventArgs e) { txtCustomerName.Text = ""; txtAddress.Text = ""; txtCity.Text = ""; txtState.Text = ""; txtZIPCode.Text = ""; txtMake.Text = ""; txtModel.Text = ""; txtYear.Text = ""; txtProblemDescription.Text = ""; dgvPartsUsed.Rows.Clear(); dgvJobsPerformed.Rows.Clear(); txtTotalParts.Text = "0.00"; txtTotalLabor.Text = "0.00"; txtTaxRate.Text = "7.75"; txtTaxAmount.Text = "0.00"; txtRepairTotal.Text = "0.00"; txtRecommendations.Text = ""; txtCustomerName.Focus(); }
- In the top section of the file, type the following: using System; using System.ComponentModel; using System.Collections; using System.Windows.Forms; using System.Data; using System.Drawing; using System.Linq; using System.IO; using System.Runtime.Serialization.Formatters.Binary;
- Return to the form
- On the form, click File and double-click Save As...
- Implement its Click event as follows: private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { // Just in case, calculate the order now CalculateTotal(); // Check the folder that will contain the repair order. // If it exists, don't create it. // If it doesn't exist, then create it string strDirectory = @"C:\College Park Auto Repair"; DirectoryInfo dirInfo = Directory.CreateDirectory(strDirectory); // Prepare to create a repair order RepairOrder order = new RepairOrder(); // Display the Save As dialog box. // Select the above directory dlgSave.InitialDirectory = strDirectory; // Check if the user clicked OK if (dlgSave.ShowDialog() == System.Windows.Forms.DialogResult.OK) { // If the user clicked OK, create a repair order order.CustomerName = txtCustomerName.Text; order.Address = txtAddress.Text; order.City = txtCity.Text; order.State = txtState.Text; order.ZIPCode = txtZIPCode.Text; order.Make = txtMake.Text; order.Model = txtModel.Text; order.Year = int.Parse(txtYear.Text); order.ProblemDescription = txtProblemDescription.Text; // Check whether the Parts Used data grid view contains some records. // If it does, save them if (dgvPartsUsed.Rows.Count > 1) { List<Part> parts = new List<Part>(); for (int row = 0; row < dgvPartsUsed.Rows.Count - 1; row++ ) { Part prt = new Part(); prt.PartName = dgvPartsUsed.Rows[row].Cells[0].EditedFormattedValue.ToString(); prt.UnitPrice = double.Parse(dgvPartsUsed.Rows[row].Cells[1].EditedFormattedValue.ToString()); prt.Quantity = int.Parse(dgvPartsUsed.Rows[row].Cells[2].EditedFormattedValue.ToString()); prt.SubTotal = double.Parse(dgvPartsUsed.Rows[row].Cells[3].EditedFormattedValue.ToString()); parts.Add(prt); } order.Parts = parts; } else // If the data grid view is empty, flag its value as null order.Parts = null; if (dgvJobsPerformed.Rows.Count > 1) { List<JobPerformed> work = new List<JobPerformed>(); for (int row = 0; row < dgvJobsPerformed.Rows.Count - 1; row++) { JobPerformed done = new JobPerformed(); done.Job = dgvJobsPerformed.Rows[row].Cells[0].EditedFormattedValue.ToString(); done.Cost = double.Parse(dgvJobsPerformed.Rows[row].Cells[1].EditedFormattedValue.ToString()); work.Add(done); } order.Jobs = work; } order.TotalParts = double.Parse(txtTotalParts.Text); order.TotalLabor = double.Parse(txtTotalLabor.Text); order.TaxRate = double.Parse(txtTaxRate.Text); order.TaxAmount = double.Parse(txtTaxAmount.Text); order.RepairTotal = double.Parse(txtRepairTotal.Text); order.Recommendations = txtRecommendations.Text; FileStream stmRepair = new FileStream(dlgSave.FileName, FileMode.Create); BinaryFormatter bfmRepair = new BinaryFormatter(); bfmRepair.Serialize(stmRepair, order); stmRepair.Close(); newToolStripMenuItem_Click(sender, e); } }
- Return to the form
- On the form, click File and double-click Open...
- Implement the event as follows: private void openToolStripMenuItem_Click(object sender, EventArgs e) { dlgOpen.InitialDirectory = @"C:\College Park Auto Repair"; if (dlgOpen.ShowDialog() == DialogResult.OK) { FileStream stmRepair = new FileStream(dlgOpen.FileName, FileMode.Open); BinaryFormatter bnrRepair = new BinaryFormatter(); RepairOrder order = (RepairOrder)bnrRepair.Deserialize(stmRepair); txtCustomerName.Text = order.CustomerName; txtAddress.Text = order.Address; txtCity.Text = order.City; txtState.Text = order.State; txtZIPCode.Text = order.ZIPCode; txtMake.Text = order.Make; txtModel.Text = order.Model; txtYear.Text = order.Year.ToString(); txtProblemDescription.Text = order.ProblemDescription; int i = 0; foreach (Part prt in order.Parts) { dgvPartsUsed.Rows.Add(); dgvPartsUsed.Rows[i].Cells[0].Value = prt.PartName; dgvPartsUsed.Rows[i].Cells[1].Value = prt.UnitPrice.ToString("F"); dgvPartsUsed.Rows[i].Cells[2].Value = prt.Quantity.ToString(); dgvPartsUsed.Rows[i].Cells[3].Value = prt.SubTotal.ToString("F"); i++; } i = 0; foreach (JobPerformed jp in order.Jobs) { dgvJobsPerformed.Rows.Add(); dgvJobsPerformed.Rows[i].Cells[0].Value = jp.Job; dgvJobsPerformed.Rows[i].Cells[1].Value = jp.Cost; i++; } txtTotalParts.Text = order.TotalParts.ToString("F"); txtTotalLabor.Text = order.TotalLabor.ToString("F"); txtTaxRate.Text = order.TaxRate.ToString("F"); txtTaxAmount.Text = order.TaxAmount.ToString("F"); txtRepairTotal.Text = order.RepairTotal.ToString("F"); txtRecommendations.Text = order.Recommendations; stmRepair.Close(); } }
- On the form, click one of the multiline text boxes
- In the Properties window, click ScrollBars, then click the arrow of its field, and select Vertical
- On the form, click the other multiline text boxes
- In the Properties window, double-click ScrollBars until it displays Vertical
- To execute the application, press F5
- Create a repair order. Here is an example:
- On the main menu of the form, click File -> Save As...
- Set the name to 100001 and click Save
- Create another order, calculate it and save it
- Try opening a previously saved order
- Close the form and return to your programming environment
Main Page Copyright © 2010-2011 C#Key

