BIM211 – Visual Programming. Database Operations II. Contents. Adding a new record into database Changing an existing record Deleting a record Displaying data from multiple tables. 3. Writing the Program. Adding database file into solution Displaying students Adding new student
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
Contents • Adding a new record into database • Changing an existing record • Deleting a record • Displaying data from multiple tables
3. Writing the Program • Adding database file into solution • Displaying students • Adding new student • Changing student info • Deleting a student • Displaying all courses a student take
Double-Click the Button and Write the Following Code: frmNewStudent frm = new frmNewStudent(); DialogResult result = frm.ShowDialog(); if (result == DialogResult.OK) { studentsTableAdapter.Fill( schoolDataSet.Students); }
Some Notes • If you close your application, make some changes in your program, build the program again, and execute the program, the last added records won’t be visible! • This is because each time you build the project, original database file is copied into the Debug folder in your solution and all operations are made on this copy.
Binding Data to the Controls • In order to fill the controls, we are going to use data binding feature instead of changing contents of the controls. • The frmChangeStudent form needs the StudentID to display the data of the selected student. • For this purpose, create a property in frmChangeStudent form. This property will transfer student ID from main form to frmChangeStudent form.
“FillByStudentID” instead of “Fill” • We want to display the data of only one student. • So, we need to use FillByStudentID method instead of Fill method. • Delete the line and write this: this.studentsTableAdapter.FillByStudentID(this.schoolDataSet.Students,this.StudentID);
OK Button • When the form is displayed, the data of the student is displayed on the controls. • User changes these data and presses OK button. • So, we need to write updating code into the Click event of the OK button.
Updating to Database • The modifications made by the used must be applied to the data set. This is accomplished by the EndEdit() method of the BindingSource object. • The changes on the data set is applied to the database by the Update() method of the studentTableAdapter object.
OK Button Click Event • Double-click the OK button and write this code: this.studentsBindingSource.EndEdit(); this.studentsTableAdapter.Update(this.schoolDataSet.Students);
Passing Student ID • Now, the form can display and update the data of the student whose ID is specified by the StudentID property. • So, we need to set this property before the form is shown. • This should be done in the main form.
Getting the ID of the selected student • The ID of the student selected from the DataGridView object can be obtained in two ways: • Get the ID from the first cell of the selected row or the DataGridView. • Get the ID from the binding source object.
1. Getting ID from DataGridView int studentID =(int)dataGridView1.SelectedRows[0].Cells[0].Value; • In order this code to be successfully executed, you need to set MultiSelect property of the DataGridView object to False and SelectionMode property to FullRowSelect.
2. Getting ID from Binding Source • When a row is selected in the DataGridView object, the information about the selected row is stored in binding source object. You can get the StudentID by using this code: DataRowViewrowView = (DataRowView)studentsBindingSource.Current; SchoolDataSet.StudentsRowrow = (SchoolDataSet.StudentsRow)rowView.Row; intstudentID = row.StudentID;