2021-04-01 21:30:29 +04:00
|
|
|
|
using DesktopTools.Enums;
|
|
|
|
|
using DesktopTools.Helpers;
|
|
|
|
|
using DesktopTools.Interfaces;
|
|
|
|
|
using DesktopTools.Models;
|
|
|
|
|
using ModuleTools.Attributes;
|
|
|
|
|
using ModuleTools.BindingModels;
|
|
|
|
|
using ModuleTools.BusinessLogics;
|
2021-04-02 09:29:09 +04:00
|
|
|
|
using ModuleTools.Enums;
|
2021-04-01 21:30:29 +04:00
|
|
|
|
using ModuleTools.Extensions;
|
|
|
|
|
using ModuleTools.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Windows.Forms;
|
2021-04-02 09:29:09 +04:00
|
|
|
|
using System.Xml.Linq;
|
2021-04-01 21:30:29 +04:00
|
|
|
|
|
|
|
|
|
namespace DesktopTools.Controls
|
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
public partial class GenericControlEntityList<G, S, L, E, BL> : MainControlViewEntityList, IControlViewEntityList
|
2021-04-01 21:30:29 +04:00
|
|
|
|
where G : GetBindingModel, new()
|
|
|
|
|
where S : SetBindingModel, new()
|
|
|
|
|
where L : ListViewModel<E>
|
|
|
|
|
where E : ElementViewModel
|
|
|
|
|
where BL : GenericBusinessLogic<G, S, L, E>
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Режим работы
|
|
|
|
|
/// </summary>
|
|
|
|
|
private ControlOpenMode _openMode;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Событие, вызываемое при закрытии контрола
|
|
|
|
|
/// </summary>
|
|
|
|
|
private event Action<Guid> CloseListEvent;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Событие, вызываемое при закрытии контрола
|
|
|
|
|
/// </summary>
|
|
|
|
|
private event Action<bool> CloseSelectEvent;
|
|
|
|
|
|
2021-04-02 11:59:07 +04:00
|
|
|
|
private readonly object _lockObject = new();
|
|
|
|
|
|
2021-04-11 20:49:26 +04:00
|
|
|
|
private bool _usePages = false;
|
|
|
|
|
|
|
|
|
|
private bool _useNames = false;
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Объект бизнес-логики для получения данных
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected BL _businessLogic;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Методы для реализации в generic-контроле
|
|
|
|
|
/// </summary>
|
2021-04-04 21:09:39 +04:00
|
|
|
|
protected IGenericControlEntityList _genericControlViewEntityList;
|
2021-04-01 21:30:29 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Констркутор
|
|
|
|
|
/// </summary>
|
|
|
|
|
public GenericControlEntityList()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
InitEvents();
|
|
|
|
|
_businessLogic = DependencyManager.Instance.Resolve<BL>();
|
|
|
|
|
_controlViewEntityList = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region IControlViewEntityList
|
2022-03-15 22:02:13 +04:00
|
|
|
|
public void OpenControl(ControlOpenModel model)
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
_openMode = model.OpenMode;
|
|
|
|
|
if (model.CloseList != null)
|
|
|
|
|
{
|
|
|
|
|
CloseListEvent += model.CloseList;
|
|
|
|
|
}
|
|
|
|
|
if (model.CloseSelect != null)
|
|
|
|
|
{
|
|
|
|
|
CloseSelectEvent += model.CloseSelect;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dataGridViewList.Columns.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Configurate(GetConfig());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2021-04-03 10:47:31 +04:00
|
|
|
|
DialogHelper.MessageException(ex.Message, $"{Title}. Ошибка при конфигурации");
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-02 11:59:07 +04:00
|
|
|
|
if (!model.LazyLoading)
|
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
LoadList();
|
2021-04-02 11:59:07 +04:00
|
|
|
|
}
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IControl GetInstanceControl() => _genericControlViewEntityList?.GetInstanceGenericControl();
|
|
|
|
|
|
2022-03-15 22:02:13 +04:00
|
|
|
|
public string GetTitleFromIdControl(Guid id) => _businessLogic.GetElement(new G { Id = id })?.ToString();
|
2021-04-02 09:29:09 +04:00
|
|
|
|
|
|
|
|
|
public string SaveControlToXml() => new XElement("Control",
|
|
|
|
|
new XAttribute("Type", GetType().FullName),
|
|
|
|
|
new XAttribute("ControlId", ControlId),
|
|
|
|
|
new XAttribute("Title", Title),
|
|
|
|
|
new XAttribute("AccessOperation", AccessOperation),
|
|
|
|
|
new XElement("Configurate",
|
|
|
|
|
new XElement("Page", toolStripTextBoxPage.Text),
|
|
|
|
|
new XElement("CountElementsOnPage", toolStripTextBoxCountRecords.Text),
|
|
|
|
|
new XElement("PageName",
|
|
|
|
|
new XElement("Key", ((toolStripComboBoxPageNames.SelectedItem as PageNamesForPaginationModel)?.Key ?? "-")),
|
|
|
|
|
new XElement("Value", ((toolStripComboBoxPageNames.SelectedItem as PageNamesForPaginationModel)?.Value ?? "-"))),
|
|
|
|
|
new XElement("ParentId", ParentId?.ToString() ?? "-"))).ToString();
|
|
|
|
|
|
2022-03-15 22:02:13 +04:00
|
|
|
|
public void LoadControlFromXml(string xml)
|
2021-04-02 09:29:09 +04:00
|
|
|
|
{
|
2021-04-02 11:59:07 +04:00
|
|
|
|
var control = XElement.Parse(xml);
|
2021-04-02 09:29:09 +04:00
|
|
|
|
ControlId = new Guid(control.Attribute("ControlId").Value.ToString());
|
|
|
|
|
Title = control.Attribute("Title").Value.ToString();
|
|
|
|
|
AccessOperation = (AccessOperation)Enum.Parse(typeof(AccessOperation), control.Attribute("AccessOperation").Value.ToString());
|
|
|
|
|
var config = control.Element("Configurate");
|
|
|
|
|
toolStripTextBoxPage.Text = config.Element("Page").Value.ToString();
|
|
|
|
|
toolStripTextBoxCountRecords.Text = config.Element("CountElementsOnPage").Value.ToString();
|
|
|
|
|
if (config.Element("PageName").Element("Key").Value.ToString() != "-")
|
|
|
|
|
{
|
|
|
|
|
var pageName = new PageNamesForPaginationModel
|
|
|
|
|
{
|
|
|
|
|
Key = config.Element("PageName").Element("Key").Value.ToString(),
|
|
|
|
|
Value = config.Element("PageName").Element("Value").Value.ToString()
|
|
|
|
|
};
|
2021-04-02 11:59:07 +04:00
|
|
|
|
var elem = toolStripComboBoxPageNames.Items.Cast<PageNamesForPaginationModel>().FirstOrDefault(x => x.Key == pageName.Key);
|
|
|
|
|
if (elem != null)
|
|
|
|
|
{
|
|
|
|
|
toolStripComboBoxPageNames.SelectedIndexChanged -= ToolStripComboBoxPageNamesSelectedIndexChanged;
|
|
|
|
|
toolStripComboBoxPageNames.SelectedItem = elem;
|
|
|
|
|
toolStripComboBoxPageNames.SelectedIndexChanged += ToolStripComboBoxPageNamesSelectedIndexChanged;
|
|
|
|
|
}
|
2021-04-02 09:29:09 +04:00
|
|
|
|
}
|
|
|
|
|
if (config.Element("ParentId").Value.ToString() != "-")
|
|
|
|
|
{
|
|
|
|
|
ParentId = new Guid(config.Element("ParentId").Value.ToString());
|
|
|
|
|
}
|
2022-03-15 22:02:13 +04:00
|
|
|
|
LoadList();
|
2021-04-02 09:29:09 +04:00
|
|
|
|
}
|
2021-04-01 21:30:29 +04:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конфигуратор контрола
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model">Настройки</param>
|
|
|
|
|
private void Configurate(ControlViewEntityListConfiguration config)
|
|
|
|
|
{
|
|
|
|
|
if (config == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// формирование таблицы на основе модели
|
|
|
|
|
dataGridViewList.Columns.Clear();
|
|
|
|
|
var properties = typeof(E).GetProperties();
|
|
|
|
|
foreach (var property in properties)
|
|
|
|
|
{
|
|
|
|
|
var attr = property.GetCustomAttribute<ViewModelControlListPropertyAttribute>();
|
|
|
|
|
if (attr != null)
|
|
|
|
|
{
|
|
|
|
|
var colIndex = attr.DisplayName == "Идентификатор" ? 0 : dataGridViewList.Columns.Count;
|
|
|
|
|
dataGridViewList.Columns.Insert(colIndex, new DataGridViewTextBoxColumn
|
|
|
|
|
{
|
|
|
|
|
HeaderText = attr.DisplayName,
|
|
|
|
|
Name = string.Format("Column{0}", property.Name),
|
|
|
|
|
ReadOnly = true,
|
|
|
|
|
Visible = !attr.IsHide,
|
2021-04-03 13:50:58 +04:00
|
|
|
|
Width = attr.ColumnWidth,
|
|
|
|
|
AutoSizeMode = attr.ColumnWidth != 0 ? DataGridViewAutoSizeColumnMode.None : DataGridViewAutoSizeColumnMode.Fill
|
2021-04-01 21:30:29 +04:00
|
|
|
|
});
|
2021-04-03 13:50:58 +04:00
|
|
|
|
if (attr.DefaultCellStyleFormat.IsNotEmpty())
|
|
|
|
|
{
|
|
|
|
|
dataGridViewList.Columns[colIndex].DefaultCellStyle.Format = attr.DefaultCellStyleFormat;
|
|
|
|
|
}
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
labelTitle.Text = Title;
|
|
|
|
|
// настройка отображения основных кнопок
|
|
|
|
|
if (config.HideToolStripButton != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var elem in config.HideToolStripButton)
|
|
|
|
|
{
|
|
|
|
|
var ctrl = toolStripMenu.Items.Find(elem.ToString(), false);
|
|
|
|
|
if (ctrl != null && ctrl.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
ctrl[0].Visible = false;
|
|
|
|
|
switch (elem)
|
|
|
|
|
{
|
|
|
|
|
case ToolStripButtonListNames.toolStripButtonAdd:
|
|
|
|
|
toolStripSeparator1.Visible = false;
|
|
|
|
|
break;
|
|
|
|
|
case ToolStripButtonListNames.toolStripButtonUpd:
|
|
|
|
|
toolStripSeparator2.Visible = false;
|
|
|
|
|
break;
|
|
|
|
|
case ToolStripButtonListNames.toolStripButtonDel:
|
|
|
|
|
toolStripSeparator3.Visible = false;
|
|
|
|
|
break;
|
|
|
|
|
case ToolStripButtonListNames.toolStripButtonSearch:
|
|
|
|
|
toolStripSeparator4.Visible = false;
|
|
|
|
|
break;
|
|
|
|
|
case ToolStripButtonListNames.toolStripButtonRef:
|
|
|
|
|
toolStripSeparator5.Visible = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Загрузка подпунктов в контекстное меню и в пункт меню "Действие"
|
|
|
|
|
if (config.ControlOnMoveElem != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var elem in config.ControlOnMoveElem)
|
|
|
|
|
{
|
|
|
|
|
ToolStripMenuItem item = new() { Text = elem.Value.Title, Name = elem.Key };
|
|
|
|
|
item.Click += elem.Value.Event;
|
|
|
|
|
toolStripSplitButtonActions.DropDownItems.Add(item);
|
2021-04-08 14:13:35 +04:00
|
|
|
|
ToolStripMenuItem itemContext = new() { Text = elem.Value.Title, Name = elem.Key };
|
|
|
|
|
itemContext.Click += elem.Value.Event;
|
|
|
|
|
contextMenuStripDataGrid.Items.Add(itemContext);
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// либо скрытие пункта, если не предусмотренно подпунктов
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
toolStripSplitButtonActions.Visible = false;
|
|
|
|
|
toolStripSeparator5.Visible = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toolStripFooter.Visible = config.PaginationOn;
|
|
|
|
|
// Пагинация
|
|
|
|
|
if (config.PaginationOn)
|
|
|
|
|
{
|
|
|
|
|
// пагинация по страницам
|
|
|
|
|
if (config.CountElementsOnPage.HasValue)
|
|
|
|
|
{
|
2021-04-11 20:49:26 +04:00
|
|
|
|
_usePages = true;
|
2021-04-01 21:30:29 +04:00
|
|
|
|
toolStripTextBoxCountRecords.Text = config.CountElementsOnPage.Value.ToString();
|
|
|
|
|
toolStripLabelPageName.Visible = toolStripComboBoxPageNames.Visible = false;
|
|
|
|
|
}
|
|
|
|
|
// пагинация по названиям
|
2021-04-11 20:49:26 +04:00
|
|
|
|
if (config.PageNamesForPagination != null)
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
2021-04-11 20:49:26 +04:00
|
|
|
|
_useNames = true;
|
2021-04-01 21:30:29 +04:00
|
|
|
|
toolStripButtonPrev.Visible = toolStripLabelPage.Visible = toolStripTextBoxPage.Visible = toolStripLabelCountPages.Visible =
|
|
|
|
|
toolStripLabelCountRecords.Visible = toolStripTextBoxCountRecords.Visible = toolStripButtonNext.Visible = false;
|
|
|
|
|
|
|
|
|
|
toolStripComboBoxPageNames.Items.AddRange(config.PageNamesForPagination.ToArray());
|
2021-04-03 19:03:56 +04:00
|
|
|
|
if (toolStripComboBoxPageNames.Items.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
toolStripComboBoxPageNames.SelectedIndexChanged -= ToolStripComboBoxPageNamesSelectedIndexChanged;
|
|
|
|
|
toolStripComboBoxPageNames.SelectedIndex = 0;
|
|
|
|
|
toolStripComboBoxPageNames.SelectedIndexChanged += ToolStripComboBoxPageNamesSelectedIndexChanged;
|
|
|
|
|
}
|
2021-04-01 21:30:29 +04:00
|
|
|
|
toolStripComboBoxPageNames.Tag = config.ParentPropertyName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// при вызове контрола для выбора элемента, делаем возможность выбора только одной записи
|
|
|
|
|
dataGridViewList.MultiSelect = _openMode == ControlOpenMode.List;
|
|
|
|
|
|
|
|
|
|
// Открытие через родительский элемент
|
|
|
|
|
if (_openMode == ControlOpenMode.Child)
|
|
|
|
|
{
|
|
|
|
|
panelHeader.Visible = false;
|
|
|
|
|
toolStripButtonClose.Visible = false;
|
2021-04-11 20:49:26 +04:00
|
|
|
|
toolStripSeparator6.Visible = false;
|
|
|
|
|
toolStripButtonSelect.Visible = false;
|
|
|
|
|
}
|
|
|
|
|
// Открытие через родительский элемент
|
|
|
|
|
if (_openMode == ControlOpenMode.Select)
|
|
|
|
|
{
|
2021-04-01 21:30:29 +04:00
|
|
|
|
toolStripSeparator6.Visible = true;
|
|
|
|
|
toolStripButtonSelect.Visible = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Dock = DockStyle.Fill;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Инициализация событий к контролам
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void InitEvents()
|
|
|
|
|
{
|
|
|
|
|
toolStripButtonAdd.Click += (object sender, EventArgs e) => { AddElement(); };
|
|
|
|
|
toolStripButtonUpd.Click += (object sender, EventArgs e) => { UpdElement(); };
|
2022-03-15 22:02:13 +04:00
|
|
|
|
toolStripButtonDel.Click += (object sender, EventArgs e) => { DelElement(); };
|
2021-04-04 21:09:39 +04:00
|
|
|
|
toolStripButtonSearch.Click += (object sender, EventArgs e) => { panelSearch.Visible = !panelSearch.Visible; };
|
2022-03-15 22:02:13 +04:00
|
|
|
|
toolStripButtonRef.Click += (object sender, EventArgs e) => { LoadList(); };
|
|
|
|
|
toolStripButtonSelect.Click += (object sender, EventArgs e) => { SelectElement(); };
|
2021-04-01 21:30:29 +04:00
|
|
|
|
toolStripButtonClose.Click += (object sender, EventArgs e) =>
|
|
|
|
|
{
|
|
|
|
|
CloseListEvent?.Invoke(ControlId);
|
|
|
|
|
CloseSelectEvent?.Invoke(false);
|
|
|
|
|
Dispose();
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-15 22:02:13 +04:00
|
|
|
|
buttonSearch.Click += (object sender, EventArgs e) => { LoadList(); };
|
|
|
|
|
buttonCancelSearch.Click += (object sender, EventArgs e) => { panelSearch.Visible = false; LoadList(); };
|
2021-04-01 21:30:29 +04:00
|
|
|
|
|
2022-03-15 22:02:13 +04:00
|
|
|
|
dataGridViewList.KeyDown += (object sender, KeyEventArgs e) =>
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
switch (e.KeyCode)
|
|
|
|
|
{
|
|
|
|
|
case Keys.Insert:
|
|
|
|
|
if (toolStripButtonAdd.Visible)
|
|
|
|
|
{
|
|
|
|
|
AddElement();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Keys.Enter:
|
|
|
|
|
if (toolStripButtonUpd.Visible)
|
|
|
|
|
{
|
|
|
|
|
UpdElement();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Keys.Delete:
|
|
|
|
|
if (toolStripButtonDel.Visible)
|
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
DelElement();
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-03-15 22:02:13 +04:00
|
|
|
|
dataGridViewList.CellDoubleClick += (object sender, DataGridViewCellEventArgs e) =>
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (_openMode == ControlOpenMode.Select && dataGridViewList.SelectedRows.Count > 0)
|
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
SelectElement();
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
UpdElement();
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-15 22:02:13 +04:00
|
|
|
|
toolStripButtonPrev.Click += (object sender, EventArgs e) =>
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (int.TryParse(toolStripTextBoxPage.Text, out int page))
|
|
|
|
|
{
|
|
|
|
|
toolStripTextBoxPage.Text = (page - 1).ToString();
|
2022-03-15 22:02:13 +04:00
|
|
|
|
LoadList();
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
};
|
2022-03-15 22:02:13 +04:00
|
|
|
|
toolStripButtonNext.Click += (object sender, EventArgs e) =>
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (int.TryParse(toolStripTextBoxPage.Text, out int page))
|
|
|
|
|
{
|
|
|
|
|
toolStripTextBoxPage.Text = (page + 1).ToString();
|
2022-03-15 22:02:13 +04:00
|
|
|
|
LoadList();
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
};
|
2022-03-15 22:02:13 +04:00
|
|
|
|
toolStripTextBoxPage.KeyDown += (object sender, KeyEventArgs e) =>
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (e.KeyData == Keys.Enter)
|
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
LoadList();
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-02 11:59:07 +04:00
|
|
|
|
toolStripComboBoxPageNames.SelectedIndexChanged += ToolStripComboBoxPageNamesSelectedIndexChanged;
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 11:59:07 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Событие смены выбрарнного элемента выпдающего спика (вынесено отдельно, чтобы при настройки конфигурации не вызывать прогрузку)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
private void ToolStripComboBoxPageNamesSelectedIndexChanged(object sender, EventArgs e) => LoadList();
|
2021-04-02 11:59:07 +04:00
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Вызов события загрузки данных на datagrid
|
|
|
|
|
/// </summary>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
private void LoadList()
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
var cursor = Cursor.Current;
|
|
|
|
|
|
|
|
|
|
var selectedGuids = new List<Guid>();
|
|
|
|
|
foreach (DataGridViewRow row in dataGridViewList.SelectedRows)
|
|
|
|
|
{
|
|
|
|
|
selectedGuids.Add(new Guid(row.Cells[0].Value.ToString()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
L data = null;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Cursor.Current = Cursors.WaitCursor;
|
2021-04-04 21:09:39 +04:00
|
|
|
|
var model = new G();
|
2021-04-01 21:30:29 +04:00
|
|
|
|
if (panelSearch.Visible)
|
|
|
|
|
{
|
|
|
|
|
var controls = panelSearchControls.Controls.Cast<IBaseControl>();
|
|
|
|
|
foreach (var cntrl in controls)
|
|
|
|
|
{
|
2021-04-04 21:09:39 +04:00
|
|
|
|
var prop = typeof(G).GetProperty(cntrl.GetPropertyName());
|
|
|
|
|
if (prop != null)
|
|
|
|
|
{
|
|
|
|
|
prop.SetValue(model, cntrl.GetValueFromControl());
|
|
|
|
|
}
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ParentPropertyName.IsNotEmpty())
|
|
|
|
|
{
|
2021-04-02 09:29:09 +04:00
|
|
|
|
if (ParentId.HasValue)
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
2021-04-04 21:09:39 +04:00
|
|
|
|
var prop = typeof(G).GetProperty(ParentPropertyName);
|
|
|
|
|
if (prop != null)
|
|
|
|
|
{
|
|
|
|
|
prop.SetValue(model, ParentId);
|
|
|
|
|
}
|
2021-04-01 21:30:29 +04:00
|
|
|
|
Visible = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Visible = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// если включена пагинация
|
2021-04-11 20:49:26 +04:00
|
|
|
|
// постраничная
|
|
|
|
|
if (_usePages)
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
2021-04-11 20:49:26 +04:00
|
|
|
|
if (int.TryParse(toolStripTextBoxPage.Text, out int page) && int.TryParse(toolStripTextBoxCountRecords.Text.ToString(), out int count))
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
2021-04-11 20:49:26 +04:00
|
|
|
|
model.PageNumber = page - 1;
|
|
|
|
|
model.PageSize = count;
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
2021-04-11 20:49:26 +04:00
|
|
|
|
}
|
|
|
|
|
// поименная
|
|
|
|
|
if (_useNames)
|
|
|
|
|
{
|
|
|
|
|
if (toolStripComboBoxPageNames.SelectedItem is PageNamesForPaginationModel key)
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
2021-04-11 20:49:26 +04:00
|
|
|
|
var prop = typeof(G).GetProperty(toolStripComboBoxPageNames.Tag.ToString());
|
|
|
|
|
if (prop != null)
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
2021-04-11 20:49:26 +04:00
|
|
|
|
prop.SetValue(model, key.Key);
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-03-15 22:02:13 +04:00
|
|
|
|
data = _businessLogic.GetList(model);
|
2021-04-06 22:07:11 +04:00
|
|
|
|
if (data == null && _businessLogic.Errors.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
DialogHelper.MessageException(_businessLogic.Errors, $"{Title}. Ошибки при получении данных");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-04-01 21:30:29 +04:00
|
|
|
|
toolStripLabelCountPages.Text = $"из {data?.MaxCount}";
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (_businessLogic.Errors.Count > 0)
|
|
|
|
|
{
|
2021-04-03 10:47:31 +04:00
|
|
|
|
DialogHelper.MessageException(_businessLogic.Errors, $"{Title}. Ошибки при получении данных");
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-04-03 10:47:31 +04:00
|
|
|
|
DialogHelper.MessageException(ex.Message, $"{Title}. Ошибка при получении данных");
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Cursor.Current = cursor;
|
|
|
|
|
}
|
2022-03-15 22:02:13 +04:00
|
|
|
|
FillDataOnGrid(data?.List, selectedGuids);
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Заполнение таблицы
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data"></param>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
private void FillDataOnGrid(List<E> data, List<Guid> selectedGuids)
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (data == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
dataGridViewList.Rows.Clear();
|
|
|
|
|
foreach (var elem in data)
|
|
|
|
|
{
|
|
|
|
|
var mas = new List<object>();
|
|
|
|
|
foreach (DataGridViewColumn column in dataGridViewList.Columns)
|
|
|
|
|
{
|
|
|
|
|
mas.Add(elem.GetType().GetProperty(column.Name["Column".Length..])?.GetValue(elem));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dataGridViewList.Rows.Add(mas.ToArray());
|
|
|
|
|
dataGridViewList.Rows[^1].Selected = selectedGuids.Contains(new Guid(mas[0].ToString()));
|
|
|
|
|
}
|
|
|
|
|
if (selectedGuids.Count == 0 && dataGridViewList.Rows.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
dataGridViewList.Rows[0].Selected = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Вызов события при добавлении элемента
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void AddElement() => LaunchControl(null);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Вызов события при изменении элемента
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void UpdElement()
|
|
|
|
|
{
|
|
|
|
|
foreach (DataGridViewRow selected in dataGridViewList.SelectedRows)
|
|
|
|
|
{
|
|
|
|
|
LaunchControl(new Guid(selected.Cells[0].Value.ToString()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Вызов события при удалении элемента
|
|
|
|
|
/// </summary>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
private void DelElement()
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (MessageBox.Show("Удалить выбранные записи?", "Подтверждение", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
|
|
|
|
|
{
|
|
|
|
|
var cursor = Cursor.Current;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Cursor.Current = Cursors.WaitCursor;
|
|
|
|
|
foreach (DataGridViewRow selected in dataGridViewList.SelectedRows)
|
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
_businessLogic.Delete(new G { Id = new Guid(selected.Cells[0].Value.ToString()) });
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
if (_businessLogic.Errors.Count != 0)
|
|
|
|
|
{
|
2021-04-03 10:47:31 +04:00
|
|
|
|
DialogHelper.MessageException(_businessLogic.Errors, $"{Title}. Ошибки при удалении");
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-04-03 10:47:31 +04:00
|
|
|
|
DialogHelper.MessageException(ex.Message, $"{Title}. Ошибки при удалении");
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Cursor.Current = cursor;
|
2022-03-15 22:02:13 +04:00
|
|
|
|
LoadList();
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Вызов события при выборе элемента
|
|
|
|
|
/// </summary>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
private void SelectElement()
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
SelectedId = new Guid(dataGridViewList.SelectedRows[0].Cells[0].Value.ToString());
|
2022-03-15 22:02:13 +04:00
|
|
|
|
SelectedText = _businessLogic.GetElement(new G { Id = SelectedId })?.ToString();
|
2021-04-01 21:30:29 +04:00
|
|
|
|
CloseSelectEvent?.Invoke(true);
|
|
|
|
|
Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2021-04-03 10:47:31 +04:00
|
|
|
|
DialogHelper.MessageException(ex.Message, $"{Title}. Ошибка при получении выбранного значения");
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание формы с контролом для работы с элементом
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
private void LaunchControl(Guid? id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var control = ControlViewEntityElement.GetInstance() as IControlChildEntity;
|
2021-04-02 09:29:09 +04:00
|
|
|
|
if (ParentId.HasValue)
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
2021-04-02 09:29:09 +04:00
|
|
|
|
control.ParentId = ParentId;
|
2021-04-01 21:30:29 +04:00
|
|
|
|
control.ParentPropertyName = ParentPropertyName;
|
|
|
|
|
}
|
2021-04-11 20:49:26 +04:00
|
|
|
|
else if (toolStripFooter.Visible && toolStripComboBoxPageNames.Visible)
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (toolStripComboBoxPageNames.SelectedItem is PageNamesForPaginationModel key)
|
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
if (key.Key.GetType().Name == "Guid")
|
|
|
|
|
{
|
|
|
|
|
control.ParentId = new Guid(key.Key.ToString());
|
|
|
|
|
}
|
2021-04-01 21:30:29 +04:00
|
|
|
|
control.ParentPropertyName = toolStripComboBoxPageNames.Tag?.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var form = new Form
|
|
|
|
|
{
|
|
|
|
|
Text = (id.HasValue ? $"{Title}. Редактирование" : $"{Title}. Добавление"),
|
|
|
|
|
StartPosition = FormStartPosition.CenterScreen,
|
|
|
|
|
ControlBox = false
|
|
|
|
|
};
|
2021-04-03 13:50:58 +04:00
|
|
|
|
control.Open(new ControlOpenModel
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
|
|
|
|
OpenMode = ControlOpenMode.List,
|
|
|
|
|
ElementId = id,
|
2022-03-15 22:02:13 +04:00
|
|
|
|
CloseElement = (Guid id) =>
|
2021-04-01 21:30:29 +04:00
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
LoadList();
|
2021-04-01 21:30:29 +04:00
|
|
|
|
form.Close();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
form.Height = (control as UserControl).Height + 55;
|
|
|
|
|
form.Width = (control as UserControl).Width + 20;
|
|
|
|
|
form.Controls.Add(control as UserControl);
|
|
|
|
|
form.Show();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2021-04-03 10:47:31 +04:00
|
|
|
|
DialogHelper.MessageException(ex.Message, $"{Title}. Ошибки при открытии элемента");
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-11 20:49:26 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение настроек для контрола
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2021-04-01 21:30:29 +04:00
|
|
|
|
private ControlViewEntityListConfiguration GetConfig() => _genericControlViewEntityList?.GetConfigControl();
|
2021-04-11 20:49:26 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение списка идентификаторов выбранных записей
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected List<Guid> GetSelectedIds()
|
|
|
|
|
{
|
|
|
|
|
List<Guid> guids = new();
|
|
|
|
|
foreach (DataGridViewRow selected in dataGridViewList.SelectedRows)
|
|
|
|
|
{
|
|
|
|
|
guids.Add(new Guid(selected.Cells[0].Value.ToString()));
|
|
|
|
|
}
|
|
|
|
|
return guids;
|
|
|
|
|
}
|
2021-04-01 21:30:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|