public string SerializeServices() { if (dataGridView1.Rows.Count == 0) { return("NULL"); } else { ServiceList list = new ServiceList(); list.services = new List <Service>(); foreach (DataGridViewRow row in dataGridView1.Rows) { list.services.Add(new Service(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString(), row.Cells[2].Value.ToString(), row.Cells[3].Value.ToString())); } return("'" + JsonConvert.SerializeObject(list) + "'"); } }
private void AddRental_Load(object sender, EventArgs e) { dataGridView1.Columns.Add("Название", "Название"); dataGridView1.Columns.Add("Описание", "Описание"); dataGridView1.Columns.Add("Цена", "Цена"); dataGridView1.Columns.Add("Количество", "Количество"); dataGridView1.Columns.Add("Стоимость", "Стоимость"); try { using (NpgsqlConnection cn = new NpgsqlConnection(Connection.str)) { cn.Open(); //получение списка клиентов NpgsqlCommand cmd1 = new NpgsqlCommand("select cli_id as cliID, fio|| ', ' ||passport as cli_name from client_table;", cn); NpgsqlDataReader reader1 = cmd1.ExecuteReader(); DataTable dt1 = new DataTable(); dt1.Load(reader1); ClientListBox2.DataSource = dt1; ClientListBox2.DisplayMember = "cli_name"; ClientListBox2.ValueMember = "cliID"; //получение списка автомобилей NpgsqlCommand cmd2 = new NpgsqlCommand("select car_id as carID, \"name\"|| ', ' ||reg_num as car_name from car_table;", cn); NpgsqlDataReader reader2 = cmd2.ExecuteReader(); DataTable dt2 = new DataTable(); dt2.Load(reader2); CarListBox1.DataSource = dt2; CarListBox1.DisplayMember = "car_name"; CarListBox1.ValueMember = "carID"; //получение списка сотрудников NpgsqlCommand cmd3 = new NpgsqlCommand("select \"employee-position_table\".link_id as empID, fio|| ', ' ||passport as emp_name from \"employee-position_table\" join employee_table ON employee_table.emp_id=\"employee-position_table\".emp_id join position_table ON position_table.pos_id=\"employee-position_table\".pos_id WHERE position_table.\"name\"='Менеджер по работе с клиентами';", cn); NpgsqlDataReader reader3 = cmd3.ExecuteReader(); DataTable dt3 = new DataTable(); dt3.Load(reader3); EmployeesListBox1.DataSource = dt3; EmployeesListBox1.DisplayMember = "emp_name"; EmployeesListBox1.ValueMember = "empID"; //получение списка областей NpgsqlCommand cmd5 = new NpgsqlCommand("select reg_id as regID, \"name\" as reg_name from region_table;", cn); NpgsqlDataReader reader5 = cmd5.ExecuteReader(); DataTable dt5 = new DataTable(); dt5.Load(reader5); RegioncomboBox1.DataSource = dt5; RegioncomboBox1.DisplayMember = "reg_name"; RegioncomboBox1.ValueMember = "regID"; if (EditMode) { //получение списка услуг NpgsqlCommand cmd4 = new NpgsqlCommand("select services from rental_table WHERE rent_id=" + rentID + ";", cn); string services = cmd4.ExecuteScalar().ToString(); ServiceList list = JsonConvert.DeserializeObject <ServiceList>(services); if (list != null) { foreach (var service in list.services) { dataGridView1.Rows.Add(service.Name, service.Description, service.Price, service.Count, Convert.ToInt32(service.Price) * Convert.ToInt32(service.Count)); } } for (int i = 0; i < dt1.Rows.Count; i++) { if (dt1.Rows[i][0].ToString() == cliID) { ClientListBox2.SelectedIndex = i; break; } } for (int i = 0; i < dt2.Rows.Count; i++) { if (dt2.Rows[i][0].ToString() == carID) { CarListBox1.SelectedIndex = i; break; } } for (int i = 0; i < dt3.Rows.Count; i++) { if (dt3.Rows[i][0].ToString() == empID) { EmployeesListBox1.SelectedIndex = i; break; } } for (int i = 0; i < dt5.Rows.Count; i++) { if (dt5.Rows[i][0].ToString() == regID) { RegioncomboBox1.SelectedIndex = i; break; } } } cn.Close(); } RefreshPrice(false); } catch (Exception er) { MessageBox.Show(er.Message); } }