/// <summary> /// Initialize datagridview control and associated data table /// </summary> /// <param name="dataGridView"></param> /// <param name="dataSet"></param> /// <param name="tableName"></param> public void InitializeDataGridViewAndDataSet(DataGridView dataGridView, DataSet dataSet, string tableName) { //get the table filled from the database DataTable table = registrationDB.GetDataTable(tableName); //set the datasource to the tabel dataGridView.DataSource = table; //autosize columns mode set to fill dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; //multiselect set to true dataGridView.MultiSelect = true; //this is for datagridviewstudent if (table.Columns[0].AutoIncrement == true) { dataGridView.DefaultValuesNeeded += (s, e) => NewRowBeingAdded(s as DataGridView, e); } //handle insertion table.RowChanged += Table_RowChanged; //handle any column changes (cells) table.ColumnChanged += Table_ColumnChanged; //handle deltion table.RowDeleted += Table_RowDeleted; dataSet.Tables.Add(table); //for backup and restore }
public StudentRegistrationAppForm() { InitializeComponent(); // form display name Text = "Student Registration App"; // get a new access layer and dataset registrationDB = new SqlDataTableAccessLayer(); registrationDataSet = new DataSet() { // must be named for backup purposes DataSetName = "StudentRegistrationDataSet", }; // YOUR CODE HERE // set the connectionString from App.config string connectingString = registrationDB.GetConnectionString("StudentRegistrationDB"); registrationDB.OpenConnection(connectingString); // set the form title Text = "Student Registration App"; // associate the datagridview controls with a database table // this also adds each table to the dataset // make sure Orders is added to database last or database restore will crash InitializeDataGridViewAndDataSet(dataGridViewDepartments, registrationDataSet, "Departments"); InitializeDataGridViewAndDataSet(dataGridViewStudents, registrationDataSet, "Students"); // DepartmentMajorCount is a view, do not add it to DataSet.Tables dataGridViewDepartmentMajorsCount.ReadOnly = true; dataGridViewDepartmentMajorsCount.AllowUserToAddRows = false; dataGridViewDepartmentMajorsCount.RowHeadersVisible = false; dataGridViewDepartmentMajorsCount.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridViewDepartmentMajorsCount.DataSource = registrationDB.GetDataTable("DepartmentMajorCount"); // Add button event hanlder for database backup to xml buttonBackupDatabase.Click += (s, e) => registrationDB.BackupDataSetToXML(registrationDataSet); buttonRestoreDatabaseFromBackup.Click += (s, e) => registrationDB.RestoreDataSetFromBackup(registrationDataSet); // Ensure that the connection to the db is closed this.FormClosing += (s, e) => registrationDB.CloseConnection(); }
public StudentRegistrationAppForm() { InitializeComponent(); // form display name Text = "Student Registration App"; // get a new access layer and dataset registrationDB = new SqlDataTableAccessLayer(); registrationDataSet = new DataSet() { // must be named for backup purposes DataSetName = "StudentRegistrationDataSet", }; //get the connection string from App.config and openconnection string connectionString = registrationDB.GetConnectionString("StudentRegistrationDB"); registrationDB.OpenConnection(connectionString); InitializeDataGridViewAndDataSet(dataGridViewDepartments, registrationDataSet, "Departments"); InitializeDataGridViewAndDataSet(dataGridViewStudents, registrationDataSet, "Students"); //DepartmentMajorsCount is a view dataGridViewDepartmentMajorsCount.DataSource = registrationDB.GetDataTable("DepartmentMajorsCount"); dataGridViewDepartmentMajorsCount.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; dataGridViewDepartmentMajorsCount.ReadOnly = true; dataGridViewDepartmentMajorsCount.AllowUserToAddRows = false; //user unable to edit or add rows dataGridViewDepartmentMajorsCount.RowHeadersVisible = false; //event handlers to backup and restore the database using DAL methods buttonBackupDatabase.Click += (s, e) => registrationDB.BackupDataSetToXML(registrationDataSet); buttonRestoreDatabaseFromBackup.Click += (s, e) => registrationDB.RestoreDataSetFromBackup(registrationDataSet); this.FormClosing += (s, e) => registrationDB.CloseConnection(); }
/// <summary> /// Generic Method to initialize the gridview /// </summary> /// <typeparam name="T"></typeparam> /// <param name="gridView"></param> /// <param name="columnsToHide"></param> private void InitializeDataGridView <T>(DataGridView gridView, DataSet dataSet, params string[] columnsToHide) where T : class { // Set up gridview gridView.AllowUserToAddRows = false; gridView.AllowUserToDeleteRows = false; gridView.ReadOnly = true; gridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; /* // Set event handler to delete row * gridView.UserDeletingRow += (s, e) => DeletingRow<T>(s as DataGridView, e);*/ // Binding the data gridView.DataSource = Controller <BeautySalonEntities, T> .SetBindingList(); foreach (string column in columnsToHide) { gridView.Columns[column].Visible = false; } string tableName; switch (typeof(T).Name) { case "Inventory": tableName = "Inventory"; break; case "AppointmentsView": tableName = "AppointmentsView"; break; default: EnglishPluralizationService pluralize = new EnglishPluralizationService(); tableName = pluralize.Pluralize(typeof(T).Name); break; } DataTable table = beautySalonDB.GetDataTable(tableName); dataSet.Tables.Add(table); }
private void InitializeDataGridViewAndDataSet(DataGridView dataGridView, DataSet dataSet, string tableName) { // Get the table filled with the records from the db DataTable table = registrationDB.GetDataTable(tableName); // set the datasource to the table // when control change, the table will change as well with one of the event below // so make sure to handle revelant table change events // this auto generates the column name, so no need to set them manually dataGridView.DataSource = table; // if we have an identity column, anytime a row is added we want the column to be set to -1 if (table.Columns[0].AutoIncrement == true) { dataGridView.DefaultValuesNeeded += (s, e) => NewRowBeingAdded(s as DataGridView, e); } // handle insertion table.RowChanged += (s, e) => RegistrationTableRowChanged(e); // handle updates table.ColumnChanged += (s, e) => RegistrationTableColumnChanged(e); // handle deletes table.RowDeleted += (s, e) => RegistrationTableRowDelete(e); // auto resize the columns to fill out as much as possible dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // allow multiple select to allow for deletion of multiple rows dataGridView.MultiSelect = true; // Add to table to the table collection // This is only use for backup and restore dataSet.Tables.Add(table); }