laba2 сданная

This commit is contained in:
Extrimal 2024-10-13 16:15:32 +04:00
parent ea308d86f2
commit 8aa6aee9f2
11 changed files with 96 additions and 83 deletions

View File

@ -8,10 +8,17 @@ namespace CreateVisualComponent
{ {
public class ColumnsConfiguratoin public class ColumnsConfiguratoin
{ {
public int ColumnsCount { get; set; } public List<ColumnConfig> Columns { get; set; }
public string[] ColumnName { get; set; } public ColumnsConfiguratoin()
public int[] Width { get; set; } {
public bool[] Visible { get; set; } Columns = new List<ColumnConfig>();
public string[] PropertiesObject { get; set; } }
}
public class ColumnConfig
{
public string ColumnName { get; set; }
public int Width { get; set; }
public bool Visible { get; set; }
public string PropertyObject { get; set; }
} }
} }

View File

@ -17,7 +17,6 @@ namespace CreateVisualComponent
{ {
public EventHandler? _changeEvent; public EventHandler? _changeEvent;
public EventHandler? _checkBoxEvent; public EventHandler? _checkBoxEvent;
public Exception? Error;
public InputComponent() public InputComponent()
{ {
@ -51,8 +50,8 @@ namespace CreateVisualComponent
public event EventHandler CheckBoxEvent public event EventHandler CheckBoxEvent
{ {
add { _checkBoxEvent += value; } add { _changeEvent += value; }
remove { _checkBoxEvent += value; } remove { _changeEvent -= value; }
} }
private void TextBoxDate_TextChanged(object sender, EventArgs e) private void TextBoxDate_TextChanged(object sender, EventArgs e)
@ -63,7 +62,7 @@ namespace CreateVisualComponent
private void CheckBoxDate_CheckedChanged(object sender, EventArgs e) private void CheckBoxDate_CheckedChanged(object sender, EventArgs e)
{ {
textBoxDate.Enabled = !checkBoxDate.Checked; textBoxDate.Enabled = !checkBoxDate.Checked;
_checkBoxEvent?.Invoke(sender, e); _changeEvent?.Invoke(sender, e);
} }
} }
} }

View File

@ -18,7 +18,10 @@ namespace CreateVisualComponent
} }
public int SelectedRow public int SelectedRow
{ {
get { return dataGridViewTable.SelectedRows[0].Index; } get
{
return dataGridViewTable.SelectedRows[0].Index;
}
set set
{ {
if (dataGridViewTable.SelectedRows.Count <= value || value < 0) if (dataGridViewTable.SelectedRows.Count <= value || value < 0)
@ -37,43 +40,38 @@ namespace CreateVisualComponent
} }
public void ConfigColumn(ColumnsConfiguratoin columnsData) public void ConfigColumn(ColumnsConfiguratoin columnsData)
{ {
dataGridViewTable.ColumnCount = columnsData.ColumnsCount; dataGridViewTable.ColumnCount = columnsData.Columns.Count;
for (int i = 0; i < columnsData.ColumnsCount; i++) for (int i = 0; i < columnsData.Columns.Count; i++)
{ {
dataGridViewTable.Columns[i].Name = columnsData.ColumnName[i]; var column = columnsData.Columns[i];
dataGridViewTable.Columns[i].Width = columnsData.Width[i]; dataGridViewTable.Columns[i].Name = column.ColumnName;
dataGridViewTable.Columns[i].Visible = columnsData.Visible[i]; dataGridViewTable.Columns[i].Width = column.Width;
dataGridViewTable.Columns[i].DataPropertyName = columnsData.PropertiesObject[i]; dataGridViewTable.Columns[i].Visible = column.Visible;
dataGridViewTable.Columns[i].DataPropertyName = column.PropertyObject;
} }
} }
public T GetSelectedObjectInRow<T>() where T : class, new() public T GetSelectedObjectInRow<T>() where T : class, new()
{ {
T val = new(); if (dataGridViewTable.SelectedRows.Count == 0) return null;
var val = new T();
var propertiesObj = typeof(T).GetProperties(); var propertiesObj = typeof(T).GetProperties();
var columns = dataGridViewTable.Columns.Cast<DataGridViewColumn>().ToList(); var selectedRow = dataGridViewTable.SelectedRows[0];
foreach (var property in propertiesObj) foreach (var property in propertiesObj)
{ {
var column = columns.FirstOrDefault(c => c.DataPropertyName == property.Name); var column = dataGridViewTable.Columns
.Cast<DataGridViewColumn>()
.FirstOrDefault(c => c.DataPropertyName == property.Name);
if (column != null) if (column != null)
{ {
object value = dataGridViewTable.SelectedRows[0].Cells[column.Index].Value; var cellValue = selectedRow.Cells[column.Index].Value;
Type propertyType = property.PropertyType; if (cellValue != DBNull.Value)
bool isNullable = Nullable.GetUnderlyingType(propertyType) != null;
if (isNullable)
{ {
propertyType = Nullable.GetUnderlyingType(propertyType); var convertedValue = Convert.ChangeType(cellValue, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType);
} property.SetValue(val, convertedValue);
if (value != DBNull.Value)
{
property.SetValue(val, Convert.ChangeType(value, propertyType));
}
else if (isNullable)
{
property.SetValue(val, null);
} }
} }
} }

View File

@ -33,18 +33,11 @@ namespace CreateVisualComponent
{ {
get get
{ {
return (listBoxSimple.SelectedIndex != -1 && listBoxSimple.SelectedItem != null) return listBoxSimple.SelectedItem?.ToString() ?? string.Empty;
? listBoxSimple.SelectedItem.ToString()
: string.Empty;
} }
set set
{ {
if (!string.IsNullOrEmpty(value)) listBoxSimple.SelectedIndex = listBoxSimple.FindString(value);
{
int index = listBoxSimple.FindString(value);
if (index == -1) return;
listBoxSimple.SetSelected(index, true);
}
} }
} }

View File

@ -24,13 +24,16 @@ namespace NotVisualComponent
} }
public void CreateDoc<T>(TableWithHeaderConfig<T> config) public void CreateDoc<T>(TableWithHeaderConfig<T> config)
{ {
string NullReplace = "null";
config.CheckFields(); config.CheckFields();
config.ColumnsRowsDataCount = (config.ColumnsRowsWidth.Count, config.Data.Count + 1); config.ColumnsRowsDataCount = (config.ColumnsRowsWidth.Count, config.Data.Count + 1);
IContext creator = new WorkWithExcel(); IContext creator = new WorkWithExcel();
creator.CreateHeader(config.Header); creator.CreateHeader(config.Header);
creator.CreateTableWithHeader(); creator.CreateTableWithHeader();
creator.CreateMultiHeader(config); creator.CreateMultiHeader(config);
var array = new string[config.Data.Count, config.Headers.Count]; var array = new string[config.Data.Count, config.Headers.Count];
for (var j = 0; j < config.Data.Count; j++) for (var j = 0; j < config.Data.Count; j++)
{ {
for (var i = 0; i < config.Headers.Count; i++) for (var i = 0; i < config.Headers.Count; i++)
@ -50,17 +53,17 @@ namespace NotVisualComponent
{ {
object? value = propertyInfo.GetValue(config.Data[j], null); object? value = propertyInfo.GetValue(config.Data[j], null);
array[j, i] = value == null array[j, i] = value == null
? config.NullReplace ? NullReplace
: value.ToString(); : value.ToString();
} }
else else
{ {
array[j, i] = config.NullReplace; array[j, i] = NullReplace;
} }
} }
else else
{ {
array[j, i] = config.NullReplace; array[j, i] = NullReplace;
} }
} }
} }

View File

@ -63,7 +63,7 @@ namespace NotVisualComponent.Helpers
Location legendLocation) Location legendLocation)
{ {
var chart = new DocumentFormat.OpenXml.Drawing.Charts.Chart(); var chart = new DocumentFormat.OpenXml.Drawing.Charts.Chart();
if (IsNullOrWhiteSpace(titleText)) if (!IsNullOrWhiteSpace(titleText))
{ {
chart.Append(GenerateTitle(titleText)); chart.Append(GenerateTitle(titleText));
} }

View File

@ -7,11 +7,14 @@ using NotVisualComponent.Models;
namespace NotVisualComponent.Helpers namespace NotVisualComponent.Helpers
{ {
public interface IContext : ICreator public interface IContext
{ {
void CreateTable(string[,] data); void CreateTable(string[,] data);
void CreateTableWithHeader(); void CreateTableWithHeader();
void CreateMultiHeader<T>(TableWithHeaderConfig<T> config); void CreateMultiHeader<T>(TableWithHeaderConfig<T> config);
void LoadDataToTableWithMultiHeader(string[,] data, int rowHeight); void LoadDataToTableWithMultiHeader(string[,] data, int rowHeight);
void CreateHeader(string header);
void SaveDoc(string filepath);
void CreateBarChart(ChartConfig config);
} }
} }

View File

@ -1,16 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NotVisualComponent.Models;
namespace NotVisualComponent.Helpers
{
public interface ICreator
{
void CreateHeader(string header);
void SaveDoc(string filepath);
void CreateBarChart(ChartConfig config);
}
}

View File

@ -223,7 +223,7 @@ namespace NotVisualComponent.Helpers
for (var j = 0; j < data.GetLength(1); j++) for (var j = 0; j < data.GetLength(1); j++)
CreateCell(j, (uint)(i + _index), data[i, j], 2u); CreateCell(j, (uint)(i + _index), data[i, j], 2u);
_index += (uint)data.GetLength(0); _index += (uint)data.GetLength(0) +1;
} }
private Cell CreateCell(string columnName, uint rowIndex) private Cell CreateCell(string columnName, uint rowIndex)
{ {

View File

@ -12,7 +12,6 @@ namespace NotVisualComponent.Models
public List<(int Column, int Row)>? ColumnsRowsWidth { get; set; } public List<(int Column, int Row)>? ColumnsRowsWidth { get; set; }
public List<(int ColumnIndex, int RowIndex, string Header, string PropertyName)>? Headers { get; set; } public List<(int ColumnIndex, int RowIndex, string Header, string PropertyName)>? Headers { get; set; }
public List<T>? Data { get; set; } public List<T>? Data { get; set; }
public string NullReplace { get; set; } = "null";
public void CheckFields() public void CheckFields()
{ {

View File

@ -94,24 +94,48 @@ namespace WinFormsTest
private void CreateTable() private void CreateTable()
{ {
ListOutputComponent.ConfigColumn(new() var config = new ColumnsConfiguratoin();
config.Columns.Add(new ColumnConfig
{ {
ColumnsCount = 4, ColumnName = "Id",
ColumnName = new string[] { "Id", "Name", "DeliveryStatus", "CountProduct" }, Width = 10,
Width = new int[] { 10, 150, 250, 200 }, Visible = false,
Visible = new bool[] { false, true, true, true, true }, PropertyObject = "Id"
PropertiesObject = new string[] { "Id", "Name", "DeliveryStatus", "CountProduct" }
}); });
config.Columns.Add(new ColumnConfig
{
ColumnName = "Name",
Width = 150,
Visible = true,
PropertyObject = "Name"
});
config.Columns.Add(new ColumnConfig
{
ColumnName = "DeliveryStatus",
Width = 250,
Visible = true,
PropertyObject = "DeliveryStatus"
});
config.Columns.Add(new ColumnConfig
{
ColumnName = "CountProduct",
Width = 200,
Visible = true,
PropertyObject = "CountProduct"
});
ListOutputComponent.ConfigColumn(config);
foreach (Delivery delivery in delivers) foreach (Delivery delivery in delivers)
{ {
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ {
ListOutputComponent.AddItem(delivery, delivery.Id, i); ListOutputComponent.AddItem(delivery, delivery.Id, i);
}
}
ListOutputComponent.Update(); ListOutputComponent.Update();
} }
}
}
private void ButtonShowTable_Click(object sender, EventArgs e) private void ButtonShowTable_Click(object sender, EventArgs e)
{ {
@ -140,6 +164,11 @@ namespace WinFormsTest
{ "1", "1", "1" }, { "1", "1", "1" },
{ "1", "2", "2" }, { "1", "2", "2" },
{ "1", "3", "3" } { "1", "3", "3" }
},
new string[,] {
{ "2", "1", "1" },
{ "2", "2", "2" },
{ "2", "3", "3" }
} }
} }
}); });
@ -149,31 +178,29 @@ namespace WinFormsTest
excelHardTable.CreateDoc(new TableWithHeaderConfig<Delivery> excelHardTable.CreateDoc(new TableWithHeaderConfig<Delivery>
{ {
FilePath = "C:\\Users\\sshan\\OneDrive\\Desktop\\PathKOP\\Excel2.xlsx", FilePath = "C:\\Users\\sshan\\OneDrive\\Desktop\\PathKOP\\Excel2.xlsx",
Header = "Computers", Header = "Delivers",
ColumnsRowsWidth = new List<(int Column, int Row)> { (5, 5), (10, 5), (10, 0), (5, 0), (7, 0) }, ColumnsRowsWidth = new List<(int Column, int Row)> { (5, 5), (10, 5), (10, 0), (5, 0), (7, 0) },
Headers = new List<(int ColumnIndex, int RowIndex, string Header, string PropertyName)> Headers = new List<(int ColumnIndex, int RowIndex, string Header, string PropertyName)>
{ {
(0, 0, "Id", "Id"), (0, 0, "Id", "Id"),
(1, 0, "CPU", "CPU"), (1, 0, "Name", "Name"),
(2, 0, "GPU", "GPU"), (2, 0, "DeliveryStatus", "DeliveryStatus"),
(3, 0, "RAM", "RAM"), (3, 0, "CountProduct", "CountProduct"),
}, },
Data = delivers, Data = delivers
NullReplace = "Çàëóïêà)"
}); });
} }
private void buttonDiagramSave_Click(object sender, EventArgs e) private void buttonDiagramSave_Click(object sender, EventArgs e)
{ {
var rnd = new Random();
excelDiagram.CreateDoc(new ChartConfig excelDiagram.CreateDoc(new ChartConfig
{ {
FilePath = "C:\\Users\\sshan\\OneDrive\\Desktop\\PathKOP\\Excel3.xlsx", FilePath = "C:\\Users\\sshan\\OneDrive\\Desktop\\PathKOP\\Excel3.xlsx",
Header = "Chart", Header = "Chart",
ChartTitle = "BarChart", ChartTitle = "Gistogram",
LegendLocation = NotVisualComponent.Models.Location.Top, LegendLocation = NotVisualComponent.Models.Location.Bottom,
Data = new Dictionary<string, List<(string Name, double Value)>> Data = new Dictionary<string, List<(string Name, double Value)>>
{ {
{ "Series 1", new() { ("GOVNO", rnd.Next()), ("ZALUPA", rnd.Next()), ("PENIS", rnd.Next()) } } { "Series 1", new() { ("Orange", 150), ("Apple", 200), ("Lime", 300) } }
} }
}); });
} }