private void InitializeRoomGridView() { // Automatically generate the DataGridView columns. rooms_grid.AutoGenerateColumns = true; rooms_grid.Width = 1100; rooms_grid.DataSource = Room.GetAll(); // Automatically resize the visible rows. rooms_grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders; // Set the DataGridView control's border. rooms_grid.BorderStyle = BorderStyle.Fixed3D; //Put the cells in edit mode rooms_grid.EditMode = DataGridViewEditMode.EditOnEnter; // Add delete button this.addDataGridButton("Delete", this.rooms_grid); //Add reserve button this.addDataGridButton("Reserve", this.rooms_grid); //Add update button this.addDataGridButton("Update", this.rooms_grid); rooms_grid.CellClick += new DataGridViewCellEventHandler(rooms_grid_CellClick); }
private void rooms_grid_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex == -1) { return; } if (e.ColumnIndex <= 6) { return; } var senderGrid = (DataGridView)sender; if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0) { MessageBox.Show(e.RowIndex.ToString()); string value = rooms_grid[e.ColumnIndex, e.RowIndex].Value.ToString(); int id = Convert.ToInt32(rooms_grid["id", e.RowIndex].Value); DataGridViewRow row = rooms_grid.Rows[e.RowIndex]; switch (value) { case "Delete": DeleteRoom(id); break; case "Reserve": ReserveRoom(id); break; case "Update": UpdateRoom(row); break; } } // Update grid rooms_grid.DataSource = Room.GetAll(); rooms_grid.Update(); }