C# Tutorial MSM_Murach chapter-19-slides

44
Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 1 C hapter19 How to w ork w ith bound controls and param eterized queries

Transcript of C# Tutorial MSM_Murach chapter-19-slides

Page 1: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 1

Chapter 19

How to work with bound controls and

parameterized queries

Page 2: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 2

Objectives Applied 1. Format the data in a bound text box by setting the properties for

the control. 2. Bind a combo box to a data source. 3. Use the properties and methods of the BindingSource class to

navigate and modify the rows in a dataset. 4. Create and use a parameterized query with a data source. 5. Customize a ToolStrip control by adding controls to it, deleting

controls from it, and formatting the controls that are on it. Then, code the event handlers for making these controls work.

6. Customize the appearance and operation of a DataGridView control.

7. Use a DataGridView control as part of a Master/Detail form.

Page 3: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 3

Objectives (continued) Knowledge 1. Explain why you might want to use code to work directly with the

binding source object for a data source. 2. Describe the use of a parameterized query and the ToolStrip

control that gets generated for the query.

Page 4: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 4

The dialog box for formatting a column

Page 5: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 5

A combo box that’s bound to a data source

Page 6: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 6

Combo box properties for binding DataSource DisplayMember ValueMember SelectedValue

Page 7: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 7

Common properties of the BindingSource class Position Count

Page 8: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 8

Common methods of the BindingSource class AddNew() EndEdit() CancelEdit() RemoveCurrent() MoveFirst() MovePrevious() MoveNext() MoveLast()

Page 9: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 9

A statement that adds a new row to a data source this.customersBindingSource.AddNew();

A statement that saves the changes to the current row and ends the edit

this.customersBindingSource.EndEdit();

A statement that cancels the changes to the current row

this.customersBindingSource.CancelEdit();

A statement that removes the current row from a data source

this.customersBindingSource.RemoveCurrent();

Page 10: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 10

Code that moves to the next row and displays the position and count

private void btnNext_Click(object sender, EventArgs e) { this.customersBindingSource.MoveNext(); int position = customersBindingSource.Position + 1; txtPosition.Text = position + " of " + customersBindingSource.Count; }

Page 11: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 11

The dialog box for creating a parameterized query

Page 12: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 12

The Customer Maintenance form with a toolbar

Page 13: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 13

The generated code for a parameterized query private void fillByCustomerIDToolStripButton_Click( object sender, EventArgs e) { try { this.customersTableAdapter.FillByCustomerID( this.mmaBooksDataSet.Customers, ((int)(System.Convert.ChangeType( customerIDToolStripTextBox.Text, typeof(int))))); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } }

Page 14: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 14

The same code after it has been improved private void fillByCustomerIDToolStripButton_Click( object sender, EventArgs e) { try { int customerID = Convert.ToInt32( customerIDToolStripTextBox.Text); this.customersTableAdapter.FillByCustomerID( this.mmaBooksDataSet.Customers, customerID); } catch (Exception ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); } }

Page 15: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 15

The syntax of the method for filling a table with a parameterized query

tableAdapter.QueryName(dataSet.TableName, param1 [,param2]...)

Page 16: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 16

The Items Collection Editor for a ToolStrip control

Page 17: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 17

Common properties of ToolStrip items DisplayStyle Image Text Width

Page 18: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 18

Customized toolbars

The event handler for the Cancel ToolStrip button private void bindingNavigatorCancelItem_Click( object sender, EventArgs e) { this.customersBindingSource.CancelEdit(); }

Page 19: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 19

The event handler for the Get All Customers ToolStrip button

private void fillToolStripButton_Click( object sender, EventArgs e) { try { this.customersTableAdapter.Fill( this.mmaBooksDataSet.Customers); } catch (SqlException ex) { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } }

Page 20: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 20

The event handler for the Get Customer ToolStrip button private void fillByCustomerIDToolStripButton_Click( object sender, EventArgs e) { try { int customerID = Convert.ToInt32( customerIDToolStripTextBox.Text); this.customersTableAdapter.FillByCustomerID( this.mmaBooksDataSet.Customers, customerID); if (customersBindingSource.Count == 0) MessageBox.Show("No customer with this ID. " + "Please try again.", "Customer Not Found"); } catch (FormatException) { MessageBox.Show("Customer ID must be an integer.", "Entry Error"); }

Page 21: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 21

The event handler for the Get Customer ToolStrip button (cont.) catch (SqlException ex) { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } }

Page 22: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 22

The form for the Customer Maintenance application

Page 23: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 23

The Customer Maintenance application private void Form1_Load(object sender, EventArgs e) { try { this.statesTableAdapter.Fill( this.mmaBooksDataSet.States); stateComboBox.SelectedIndex = -1; } catch (SqlException ex) { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } }

Page 24: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 24

The Customer Maintenance application (cont.) private void fillByCustomerIDToolStripButton_Click( object sender, EventArgs e) { try { int customerID = Convert.ToInt32( customerIDToolStripTextBox.Text); this.customersTableAdapter.FillByCustomerID( this.mmaBooksDataSet.Customers, customerID); if (customersBindingSource.Count == 0) MessageBox.Show("No customer with this ID. " + "Please try again.", "Customer Not Found"); }

Page 25: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 25

The Customer Maintenance application (cont.) catch (FormatException) { MessageBox.Show( "Customer ID must be an integer.", "Entry Error"); } catch (SqlException ex) { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } } private void bindingNavigatorCancelItem_Click( object sender, EventArgs e) { this.customersBindingSource.CancelEdit(); }

Page 26: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 26

The Customer Maintenance application (cont.) private void customersBindingNavigatorSaveItem_Click( object sender, EventArgs e) { if (customersBindingSource.Count > 0) { if (IsValidData()) { try { this.customersBindingSource.EndEdit(); this.tableAdapterManager.UpdateAll( this.mmaBooksDataSet); }

Page 27: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 27

The Customer Maintenance application (cont.) catch (ArgumentException ex) { MessageBox.Show(ex.Message, "Argument Exception"); customersBindingSource.CancelEdit(); } catch (DBConcurrencyException) { MessageBox.Show( "A concurrency error occurred. " + "Some rows were not updated.", "Concurrency Exception"); this.customersTableAdapter.Fill( this.mmaBooksDataSet.Customers); }

Page 28: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 28

The Customer Maintenance application (cont.) catch (DataException ex) { MessageBox.Show(ex.Message, ex.GetType().ToString()); customersBindingSource.CancelEdit(); } catch (SqlException ex) { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } } }

Page 29: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 29

The Customer Maintenance application (cont.) else { try { this.tableAdapterManager.UpdateAll( this.mmaBooksDataSet); } catch (DBConcurrencyException) { MessageBox.Show("A concurrency error occurred. " + "Some rows were not updated.", "Concurrency Exception"); this.customersTableAdapter.Fill( this.mmaBooksDataSet.Customers); } catch (SqlException ex) { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } } }

Page 30: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 30

The Customer Maintenance application (cont.) public bool IsValidData() { return IsPresent(nameTextBox, "Name") && IsPresent(addressTextBox, "Address") && IsPresent(cityTextBox, "City") && IsPresent(stateComboBox, "State") && IsPresent(zipCodeTextBox, "Zip code"); }

Page 31: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 31

The Customer Maintenance application (cont.) public bool IsPresent(Control control, string name) { if (control.GetType().ToString() == "System.Windows.Forms.TextBox") { TextBox textBox = (TextBox)control; if (textBox.Text == "") { MessageBox.Show( name + " is a required field.", "Entry Error"); textBox.Focus(); return false; } }

Page 32: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 32

The Customer Maintenance application (cont.) else if (control.GetType().ToString() == "System.Windows.Forms.ComboBox") { ComboBox comboBox = (ComboBox)control; if (comboBox.SelectedIndex == -1) { MessageBox.Show( name + " is a required field.", "Entry Error"); comboBox.Focus(); return false; } } return true; }

Page 33: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 33

The Customer Maintenance application (cont.) private void fillToolStripButton_Click( object sender, EventArgs e) { try { this.customersTableAdapter.Fill( this.mmaBooksDataSet.Customers); } catch (SqlException ex) { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } }

Page 34: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 34

The smart tag menu for a DataGridView control

Page 35: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 35

The dialog box for editing DataGridView columns

Page 36: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 36

Common properties of a column HeaderText Width DefaultCellStyle ReadOnly SortMode

Page 37: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 37

To format columns: The CellStyle Builder dialog box

Page 38: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 38

To format columns: The Format String dialog box

Page 39: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 39

A form that uses a DataGridView control

Page 40: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 40

Two BindingSource properties for displaying data from a related table DataSource DataMember

The property settings for the invoicesBindingSource object Property Setting DataSource customersBindingSource DataMember FK_Invoices_Customers

Page 41: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 41

The Customer Invoices form

Page 42: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 42

The dataset schema

Page 43: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 43

The code for the Customer Invoices form private void fillByCustomerIDToolStripButton_Click( object sender, EventArgs e) { try { int customerID = Convert.ToInt32( customerIDToolStripTextBox.Text); this.customersTableAdapter.FillByCustomerID( this.mmaBooksDataSet.Customers, customerID); if (customersBindingSource.Count > 0) this.invoicesTableAdapter.FillByCustomerID( this.mmaBooksDataSet.Invoices, customerID); else MessageBox.Show("No customer with this ID. " + "Please try again.", "Customer Not Found"); }

Page 44: C# Tutorial MSM_Murach chapter-19-slides

Murach’s C# 2010, C19 © 2010, Mike Murach & Associates, Inc. Slide 44

The code for the Customer Invoices form (cont.) catch (FormatException) { MessageBox.Show("Customer ID must be an integer.", "Entry Error"); } catch (SqlException ex) { MessageBox.Show("Database error # " + ex.Number + ": " + ex.Message, ex.GetType().ToString()); } }