123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace project
- {
- enum RowState
- {
- Existed,
- New,
- ModifiedNew,
- Deleted
- }
- public partial class Main : Form
- {
- string code, name, surname, patronymic, number, group_type, codeCard;
- DataBase dataBase = new DataBase();
- public Main()
- {
- InitializeComponent();
- }
- private void ReadSingleRow(DataGridView dgw, IDataRecord record)
- {
- dgw.Rows.Add(record.GetInt32(0), record.GetString(1), record.GetString(2), record.GetString(3), record.GetString(4), record.GetString(5), record.GetString(6));
- }
- private void RefresDataGird(DataGridView dgw)
- {
- dgw.Rows.Clear();
- string queryString = $"Select * from Student";
- SqlCommand command = new SqlCommand(queryString, dataBase.GetConnection());
- dataBase.openConnection();
- SqlDataReader reader = command.ExecuteReader();
- while (reader.Read())
- {
- ReadSingleRow(dgw, reader);
- }
- reader.Close();
- }
- private void CreateColumns()
- {
- dataGridView1.Columns.Add("code", "ID");
- dataGridView1.Columns.Add("name", "Имя");
- dataGridView1.Columns.Add("surname", "Фамилия");
- dataGridView1.Columns.Add("patronymic", "Отчество");
- dataGridView1.Columns.Add("number", "Телефон");
- dataGridView1.Columns.Add("group_type", "Группа");
- dataGridView1.Columns.Add("codeCard", "Код карты");
- dataGridView1.Columns.Add("", "");
- }
- private void Main_FormClosed(object sender, FormClosedEventArgs e)
- {
- Application.Exit();
- }
- private void Main_Load(object sender, EventArgs e)
- {
- CreateColumns();
- RefresDataGird(dataGridView1);
- dataBase.openConnection();
- button2.Visible = true;
- button4.Visible = true;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- deleteRow();
- Update();
- }
- private void deleteRow()
- {
- int index = dataGridView1.CurrentCell.RowIndex;
- dataGridView1.Rows[index].Visible = false;
- if (dataGridView1.Rows[index].Cells[0].Value.ToString() == String.Empty)
- {
- dataGridView1.Rows[index].Cells[6].Value = RowState.Deleted;
- return;
- }
- dataGridView1.Rows[index].Cells[6].Value = RowState.Deleted;
- }
- new private void Update()
- {
- dataBase.openConnection();
- for (int index = 0; index < dataGridView1.Rows.Count; index++)
- {
- try
- {
- var rowState = (RowState)dataGridView1.Rows[index].Cells[6].Value;
- if (rowState == RowState.Existed)
- continue;
- if (rowState == RowState.Deleted)
- {
- var id = Convert.ToInt32(dataGridView1.Rows[index].Cells[0].Value);
- var deleteQuery = $"delete from Student where code = {id}";
- var command = new SqlCommand(deleteQuery, dataBase.GetConnection());
- command.ExecuteNonQuery();
- }
- }
- catch { }
- }
- dataBase.closeConnection();
- }
- private void button4_Click(object sender, EventArgs e)
- {
- this.Hide();
- DannyeAdd add = new DannyeAdd();
- add.Show();
- }
- private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- var selectedRow = e.RowIndex;
- if (e.RowIndex >= 0)
- {
- DataGridViewRow row = dataGridView1.Rows[selectedRow];
- code = row.Cells[0].Value.ToString();
- name = row.Cells[1].Value.ToString();
- surname = row.Cells[2].Value.ToString();
- patronymic = row.Cells[3].Value.ToString();
- number = row.Cells[4].Value.ToString();
- group_type = row.Cells[5].Value.ToString();
- codeCard = row.Cells[6].Value.ToString();
- }
- }
- }
- }
|