208 lines
7.8 KiB
C#
208 lines
7.8 KiB
C#
using MySql.Data.MySqlClient;
|
|
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 static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
|
|
namespace test
|
|
{
|
|
enum RowState
|
|
{
|
|
Existed,
|
|
New,
|
|
Modified,
|
|
ModifiedNew,
|
|
Deleted
|
|
}
|
|
public partial class Form2 : Form
|
|
{
|
|
int SelectedRow;
|
|
string connect = "server=cfif31.ru;user=ISPr24-37_AnpilogovPV;database=ISPr24-37_AnpilogovPV_ekzam;password=ISPr24-37_AnpilogovPV";
|
|
public Form2()
|
|
{
|
|
InitializeComponent();
|
|
CreateColumns();
|
|
}
|
|
private void CreateColumns()
|
|
{
|
|
dataGridView1.Columns.Add("idproducti", "id");
|
|
dataGridView1.Columns.Add("name", "Название");
|
|
dataGridView1.Columns.Add("cena", "Цена");
|
|
dataGridView1.Columns.Add("isNew", string.Empty);
|
|
}
|
|
private void ReadSingleRow(DataGridView dgw, IDataRecord record)
|
|
{
|
|
dgw.Rows.Add(record.GetInt32(0), record.GetString(1), record.GetInt32(2), RowState.Existed);
|
|
}
|
|
private void RefreshDataGrid(DataGridView dgw)
|
|
{
|
|
dgw.Rows.Clear();
|
|
string query = "SELECT idproducti, name, cena FROM producti";
|
|
using (MySqlConnection connection = new MySqlConnection(connect))
|
|
{
|
|
try
|
|
{
|
|
connection.Open();
|
|
using (MySqlCommand command = new MySqlCommand(query, connection)) using (MySqlDataReader reader = command.ExecuteReader())
|
|
{
|
|
while (reader.Read())
|
|
{
|
|
ReadSingleRow(dgw, reader);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Error: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
private void Form2_Load(object sender, EventArgs e)
|
|
{
|
|
RefreshDataGrid(dataGridView1);
|
|
}
|
|
private void Search(DataGridView dgw)
|
|
{
|
|
dgw.Rows.Clear();
|
|
string searchString = "SELECT idproducti, name, cena FROM producti WHERE CONCAT(idproducti, name, cena) LIKE '%" + textBox1.Text + "%'";
|
|
using (MySqlConnection connection = new MySqlConnection(connect))
|
|
{
|
|
try
|
|
{
|
|
connection.Open();
|
|
using (MySqlCommand com = new MySqlCommand(searchString, connection)) using (MySqlDataReader read = com.ExecuteReader())
|
|
{
|
|
while (read.Read())
|
|
{
|
|
ReadSingleRow(dgw, read);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Error: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
private void textBox1_TextChanged(object sender, EventArgs e)
|
|
{
|
|
Search(dataGridView1);
|
|
}
|
|
private void deletRow()
|
|
{
|
|
int index = dataGridView1.CurrentCell.RowIndex;
|
|
if (index >= 0)
|
|
{
|
|
var id = dataGridView1.Rows[index].Cells[0].Value;
|
|
if (id != null && !string.IsNullOrEmpty(id.ToString()))
|
|
{
|
|
dataGridView1.Rows[index].Cells[3].Value = RowState.Deleted;
|
|
dataGridView1.Rows[index].Visible = false;
|
|
}
|
|
}
|
|
}
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
deletRow();
|
|
}
|
|
private void Save()
|
|
{
|
|
using (MySqlConnection connection = new MySqlConnection(connect))
|
|
{
|
|
try
|
|
{
|
|
connection.Open();
|
|
for (int index = 0; index < dataGridView1.Rows.Count; index++)
|
|
{
|
|
var rowState = (RowState)dataGridView1.Rows[index].Cells[3].Value;
|
|
if (rowState == RowState.Deleted)
|
|
{
|
|
var id = dataGridView1.Rows[index].Cells[0].Value;
|
|
if (id != null && !string.IsNullOrEmpty(id.ToString()))
|
|
{
|
|
using (MySqlCommand deleteCommandPlan = new MySqlCommand("DELETE FROM producti WHERE idproducti = @id", connection))
|
|
{
|
|
deleteCommandPlan.Parameters.AddWithValue("@id", id);
|
|
deleteCommandPlan.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
if (rowState == RowState.Modified)
|
|
{
|
|
var id = dataGridView1.Rows[index].Cells[0].Value.ToString();
|
|
var name = dataGridView1.Rows[index].Cells[1].Value.ToString();
|
|
var cena = dataGridView1.Rows[index].Cells[2].Value.ToString();
|
|
{
|
|
var changeQuery = "UPDATE producti SET name = @name, cena = @cena WHERE idproducti = @id";
|
|
using (var command = new MySqlCommand(changeQuery, connection))
|
|
{
|
|
command.Parameters.AddWithValue("@id", id);
|
|
command.Parameters.AddWithValue("@name", name);
|
|
command.Parameters.AddWithValue("@cena", cena);
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageBox.Show($"Error: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
private void Change()
|
|
{
|
|
var selectedRowIndex = dataGridView1.CurrentCell.RowIndex;
|
|
var id = textBox2.Text;
|
|
var name = textBox3.Text;
|
|
int cena;
|
|
if (dataGridView1.Rows[selectedRowIndex].Cells[0].Value.ToString() != string.Empty)
|
|
{
|
|
if (int.TryParse(textBox4.Text, out cena))
|
|
{
|
|
dataGridView1.Rows[selectedRowIndex].SetValues(id, name, cena);
|
|
dataGridView1.Rows[selectedRowIndex].Cells[3].Value = RowState.Modified;
|
|
}
|
|
}
|
|
}
|
|
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
|
|
{
|
|
SelectedRow = e.RowIndex;
|
|
if (e.RowIndex >= 0)
|
|
{
|
|
DataGridViewRow row = dataGridView1.Rows[SelectedRow];
|
|
textBox2.Text = row.Cells[0].Value.ToString();
|
|
textBox3.Text = row.Cells[1].Value.ToString();
|
|
textBox4.Text = row.Cells[2].Value.ToString();
|
|
}
|
|
}
|
|
private void ClearTextBox()
|
|
{
|
|
textBox2.Text = "";
|
|
textBox3.Text = "";
|
|
textBox4.Text = "";
|
|
}
|
|
private void button4_Click(object sender, EventArgs e)
|
|
{
|
|
Change();
|
|
}
|
|
private void button3_Click(object sender, EventArgs e)
|
|
{
|
|
Save();
|
|
ClearTextBox();
|
|
}
|
|
private void button2_Click(object sender, EventArgs e)
|
|
{
|
|
Form4 form4 = new Form4();
|
|
form4.Show();
|
|
}
|
|
}
|
|
}
|