c# tutorial msm_murach chapter-19-slides

Post on 06-Apr-2017

25 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

Chapter 19

How to work with bound controls and

parameterized queries

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.

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.

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

The dialog box for formatting a column

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

A combo box that’s bound to a data source

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

Combo box properties for binding DataSource DisplayMember ValueMember SelectedValue

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

Common properties of the BindingSource class Position Count

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()

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();

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; }

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

The dialog box for creating a parameterized query

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

The Customer Maintenance form with a toolbar

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); } }

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()); } }

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]...)

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

The Items Collection Editor for a ToolStrip control

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

Common properties of ToolStrip items DisplayStyle Image Text Width

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(); }

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()); } }

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"); }

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()); } }

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

The form for the Customer Maintenance application

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()); } }

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"); }

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(); }

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); }

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); }

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()); } } }

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()); } } }

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"); }

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; } }

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; }

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()); } }

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

The smart tag menu for a DataGridView control

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

The dialog box for editing DataGridView columns

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

Common properties of a column HeaderText Width DefaultCellStyle ReadOnly SortMode

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

To format columns: The CellStyle Builder dialog box

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

To format columns: The Format String dialog box

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

A form that uses a DataGridView control

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

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

The Customer Invoices form

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

The dataset schema

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"); }

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()); } }

top related