private void button5_Click(object sender, EventArgs e) { MovieShows ms = new MovieShows(); ms.ShowDialog(); /******************After adding new Movie or Show *************************/ BindGridMovieShows(); // method calling to bind customer to data grid CreateEditBtton(); //method calling to display edit button CreateDeleteBtton(); //method calling to display delete button CreateRentBtton(); // method calling to display RENT button }
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (Flag == "customer") { try { string customerID, Name, address, Mobile; if (e.ColumnIndex == 4) // edit button { customerID = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); Name = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(); address = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString(); Mobile = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString(); Cutomer cutomer = new Cutomer(customerID, Name, address, Mobile); cutomer.ShowDialog(); /******************After Updating customer*************************/ BindGridCustomer(); // method calling to bind customer to data grid CreateEditBtton(); //method calling to display edit button CreateDeleteBtton(); //method calling to display delete button } else if (e.ColumnIndex == 5) // delete button { DialogResult result = MessageBox.Show("Are you sure to delete this record?", "Alert", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { customerID = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); SqlCommand cmd = new SqlCommand(); string query = "delete customer where customerId=@customerId"; cmd.CommandText = query; cmd.Parameters.Add(new SqlParameter("@customerId", customerID)); cmd.Connection = con; bool ans = cmd.ExecuteNonQuery() > 0; if (ans) { BindGridCustomer(); // bind grid customer after deleting the customer CreateEditBtton(); //method calling to display edit button CreateDeleteBtton(); //method calling to display delete button MessageBox.Show("Customer Deleted!", "Alert"); } else { MessageBox.Show("Unable to Delete this Customer", "Alert"); } cmd.Dispose(); } } } catch (Exception ex) { if (ex.Message.Contains("REFERENCE")) { MessageBox.Show("You Can not delete this Customer as he/she is rented a movie!"); } else { MessageBox.Show(ex.Message); } } } else if (Flag == "movies") { try { string MovieID, Title, Year, Genre, WebRating, RentAmount; if (e.ColumnIndex == 6) // edit button { MovieID = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); Title = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(); Year = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString(); RentAmount = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString(); Genre = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString(); WebRating = dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString(); MovieShows ms = new MovieShows(MovieID, Title, Year, Genre, WebRating, RentAmount); ms.ShowDialog(); /******************After Updating movie*************************/ BindGridMovieShows(); // method calling to bind movies and shows to data grid CreateRentBtton(); // method calling to display RENT button CreateEditBtton(); //method calling to display edit button CreateDeleteBtton(); //method calling to display delete button } else if (e.ColumnIndex == 7) // delete button { DialogResult result = MessageBox.Show("Are you sure to delete this record?", "Alert", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { MovieID = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); SqlCommand cmd = new SqlCommand(); string query = "delete movies where MovieID=@MovieID"; cmd.CommandText = query; cmd.Parameters.Add(new SqlParameter("@MovieID", MovieID)); cmd.Connection = con; bool ans = cmd.ExecuteNonQuery() > 0; if (ans) { BindGridMovieShows(); // bind grid movie and shows after deleting the movie CreateEditBtton(); //method calling to display edit button CreateDeleteBtton(); //method calling to display delete button CreateRentBtton(); // method calling to display RENT button MessageBox.Show("Movie Deleted!", "Alert"); } else { MessageBox.Show("Unable to Delete this Movie", "Alert"); } cmd.Dispose(); } } else if (e.ColumnIndex == 8) // Rent button { gbRentMovie.Visible = true; SelectedMovieID = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); BindDdlCustomer(); // method calling to bind comboBox customer } } catch (Exception ex) { if (ex.Message.Contains("REFERENCE")) { MessageBox.Show("You Can not delete this Movie / Show as it is rented by a Customer!"); } else { MessageBox.Show(ex.Message); } } } else if (Flag == "return") { try { string RentID, MovieName, CustomerName; if (e.ColumnIndex == 7) // retrun button { RentID = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); MovieName = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString(); CustomerName = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(); DialogResult result = MessageBox.Show("Do you want to Return the Movie: " + MovieName + ", for the Customer: " + CustomerName + "?", "Alert", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { SqlCommand cmd = new SqlCommand("delete from moviesonrent where RentID=@RentID", con); cmd.Parameters.AddWithValue("@RentID", RentID); bool ans = cmd.ExecuteNonQuery() > 0; if (ans) { BindGridRentedMovieShows(); //method calling to get all rented movies and shows CreateReturnBtton(); //method calling to display return movie button MessageBox.Show("Movie returned successfully!"); } cmd.Dispose(); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }