private void dataGridGroup_CellContentClick(object sender, DataGridViewCellEventArgs e) { dataGridGroup.Rows[e.RowIndex].ReadOnly = true; int noOfRows = dataGridGroup.RowCount; if (e.ColumnIndex == 3 && e.RowIndex >= 0 && e.RowIndex != (noOfRows - 1)) // when click on del button { DialogResult dialogResult = MessageBox.Show("Are you sure that you want to delete it?", "Confirmation", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { SqlConnection con = new SqlConnection(conURL); // connection opens con.Open(); try { string cmdText = "DELETE FROM GroupStudent WHERE GroupId = @GroupId"; SqlCommand c = new SqlCommand(cmdText, con); int Idy = Convert.ToInt32(dataGridGroup.Rows[e.RowIndex].Cells[0].Value); c.Parameters.Add(new SqlParameter("@GroupId", Idy)); // execute it c.ExecuteNonQuery(); string cmdText2 = "DELETE FROM [Group] WHERE Id = @Id"; SqlCommand c2 = new SqlCommand(cmdText2, con); c2.Parameters.Add(new SqlParameter("@Id", Idy)); c2.ExecuteNonQuery(); // connection closed con.Close(); dataGridGroup.DataSource = null; dataGridGroup.Rows.Clear(); dataGridGroup.Columns.Clear(); StudentGroupDatagrid.DataSource = null; StudentGroupDatagrid.Rows.Clear(); StudentGroupDatagrid.Columns.Clear(); update(); MessageBox.Show("Successfully Deleted"); } catch (Exception) { MessageBox.Show("Project might be assign to this group or this group might be some record in evaluation. So delete that record then delete it here"); } } else if (dialogResult == DialogResult.No) { //do something else } } if (e.ColumnIndex == 2 && e.RowIndex >= 0 && e.RowIndex != (noOfRows - 1)) // when click on add button { groupID = Convert.ToInt32(dataGridGroup.Rows[e.RowIndex].Cells[0].Value); this.Hide(); AddStudent create = new AddStudent(); create.ShowDialog(); this.Close(); } else { } }
private void StudentGroupDatagrid_CellContentClick(object sender, DataGridViewCellEventArgs e) { StudentGroupDatagrid.Rows[e.RowIndex].ReadOnly = true; int noOfRows = StudentGroupDatagrid.RowCount; if (e.ColumnIndex == 5 && e.RowIndex >= 0 && e.RowIndex != (noOfRows - 1)) // when click on update button { stID = Convert.ToInt32(StudentGroupDatagrid.Rows[e.RowIndex].Cells[1].Value); groupID = Convert.ToInt32(StudentGroupDatagrid.Rows[e.RowIndex].Cells[0].Value); this.Hide(); AddStudent st = new AddStudent(); st.ShowDialog(); this.Close(); } if (e.ColumnIndex == 6 && e.RowIndex >= 0 && e.RowIndex != (noOfRows - 1)) // when click on del button { DialogResult dialogResult = MessageBox.Show("Are you sure that you want to delete it?", "Confirmation", MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { SqlConnection con = new SqlConnection(conURL); // connection opens con.Open(); try { string cmdText = "DELETE FROM GroupStudent WHERE GroupId = @GroupId AND StudentId = @StudentId"; SqlCommand c = new SqlCommand(cmdText, con); int Idy = Convert.ToInt32(StudentGroupDatagrid.Rows[e.RowIndex].Cells[0].Value); int stid = Convert.ToInt32(StudentGroupDatagrid.Rows[e.RowIndex].Cells[1].Value); c.Parameters.Add(new SqlParameter("@GroupId", Idy)); c.Parameters.Add(new SqlParameter("@StudentId", stid)); // execute it c.ExecuteNonQuery(); // connection closed con.Close(); dataGridGroup.DataSource = null; dataGridGroup.Rows.Clear(); dataGridGroup.Columns.Clear(); StudentGroupDatagrid.DataSource = null; StudentGroupDatagrid.Rows.Clear(); StudentGroupDatagrid.Columns.Clear(); update(); MessageBox.Show("Successfully Deleted"); } catch (Exception) { MessageBox.Show("Error"); } } else if (dialogResult == DialogResult.No) { //do something else } } }
private void button1_Click(object sender, EventArgs e) { AddStudent st = new AddStudent(); st.ShowDialog(); }