Осталось заполнение grid (⁠╥⁠﹏⁠╥⁠)

This commit is contained in:
Максим Яковлев 2024-09-06 01:06:10 +04:00
parent 47a5e5c726
commit 6b9f5e4ac7
6 changed files with 343 additions and 0 deletions

View File

@ -0,0 +1,65 @@
namespace ComponentProgramming
{
partial class ControlDataGrid
{
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором компонентов
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
private void InitializeComponent()
{
dataGridView = new DataGridView();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout();
//
// dataGridView
//
dataGridView.AllowUserToAddRows = false;
dataGridView.AllowUserToDeleteRows = false;
dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
dataGridView.BackgroundColor = Color.White;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Location = new Point(3, 3);
dataGridView.Name = "dataGridView";
dataGridView.ReadOnly = true;
dataGridView.RowHeadersVisible = false;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.Size = new Size(329, 229);
dataGridView.TabIndex = 0;
//
// ControlDataGrid
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
Controls.Add(dataGridView);
Name = "ControlDataGrid";
Size = new Size(335, 235);
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false);
}
#endregion
private DataGridView dataGridView;
}
}

View File

@ -0,0 +1,80 @@
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;
namespace ComponentProgramming
{
public partial class ControlDataGrid : UserControl
{
public ControlDataGrid()
{
InitializeComponent();
}
public void CreateColumns(int numCol, List<string> headers, List<int> width, List<bool> isVisible, List<string> props)
{
dataGridView.ColumnCount = numCol;
for(int i = 0; i < numCol; i++)
{
dataGridView.Columns[i].HeaderText = headers[i];
dataGridView.Columns[i].Width = width[i];
dataGridView.Columns[i].Visible = isVisible[i];
dataGridView.Columns[i].Name = props[i];
}
dataGridView.Rows.Add("1", "sadasd");
dataGridView.Rows.Add("2", "sadsadfasdfasdfasd");
}
public void ClearData()
{
dataGridView.Rows.Clear();
}
public int SelectedRow
{
get
{
if (dataGridView.SelectedRows[0] != null)
{
return dataGridView.SelectedRows[0].Index;
}
else
{
throw new Exception($"Вы не выбрали строку");
}
}
set
{
dataGridView.Columns[0].DisplayIndex = value;
}
}
public T? GetObject<T>() where T : class, new()
{
if (dataGridView.SelectedRows[0] == null)
{
return null;
}
DataGridViewRow selectedString = dataGridView.SelectedRows[0];
T obj = new T();
foreach (var prop in obj.GetType().GetProperties())
{
if (!prop.CanWrite)
{
continue;
}
prop.SetValue(obj, selectedString.Cells[$"{prop.Name}"].Value);
}
return obj;
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -30,6 +30,9 @@
{
controlComboBox = new ComponentProgramming.ControlComboBox();
controlTextBox = new ComponentProgramming.ControlTextBox();
controlDataGrid = new ComponentProgramming.ControlDataGrid();
buttonGetObj = new Button();
buttonClear = new Button();
SuspendLayout();
//
// controlComboBox
@ -49,11 +52,41 @@
controlTextBox.TabIndex = 1;
controlTextBox.CheckBoxChanged += controlTextBox_CheckBoxChanged;
//
// controlDataGrid
//
controlDataGrid.Location = new Point(12, 111);
controlDataGrid.Name = "controlDataGrid";
controlDataGrid.Size = new Size(776, 296);
controlDataGrid.TabIndex = 2;
//
// buttonGetObj
//
buttonGetObj.Location = new Point(23, 415);
buttonGetObj.Name = "buttonGetObj";
buttonGetObj.Size = new Size(126, 23);
buttonGetObj.TabIndex = 3;
buttonGetObj.Text = "Получить объект";
buttonGetObj.UseVisualStyleBackColor = true;
buttonGetObj.Click += buttonGetObj_Click;
//
// buttonClear
//
buttonClear.Location = new Point(654, 415);
buttonClear.Name = "buttonClear";
buttonClear.Size = new Size(134, 23);
buttonClear.TabIndex = 4;
buttonClear.Text = "Очистить объекты";
buttonClear.UseVisualStyleBackColor = true;
buttonClear.Click += buttonClear_Click;
//
// Form
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(buttonClear);
Controls.Add(buttonGetObj);
Controls.Add(controlDataGrid);
Controls.Add(controlTextBox);
Controls.Add(controlComboBox);
Name = "Form";
@ -66,5 +99,8 @@
private ComponentProgramming.ControlImage control;
private ComponentProgramming.ControlComboBox controlComboBox;
private ComponentProgramming.ControlTextBox controlTextBox;
private ComponentProgramming.ControlDataGrid controlDataGrid;
private Button buttonGetObj;
private Button buttonClear;
}
}

View File

@ -7,6 +7,7 @@ namespace Forms
InitializeComponent();
FillBox();
FillTextBox();
FillGrid();
}
private void FillBox()
@ -17,6 +18,14 @@ namespace Forms
{
controlTextBox.text = "+79063908075";
}
private void FillGrid()
{
List<string> headers = new List<string>() { "Id", "Èìÿ" };
List<int> width = new List<int>() { 200, 200 };
List<bool> isVisible = new List<bool>() { true, true };
List<string> props = new List<string>() { "Id", "Name" };
controlDataGrid.CreateColumns(2, headers, width, isVisible, props);
}
private void controlComboBox_ComboBoxChanged(object sender, EventArgs e)
{
@ -35,5 +44,16 @@ namespace Forms
MessageBox.Show($"CheckBox not checked");
}
}
private void buttonGetObj_Click(object sender, EventArgs e)
{
var da = controlDataGrid.GetObject<Person>();
MessageBox.Show($"{da?.Id}:{da?.Name} ");
}
private void buttonClear_Click(object sender, EventArgs e)
{
controlDataGrid.ClearData();
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Forms
{
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
public Person() { }
public Person(string name, string id)
{
Name = name;
Id = id;
}
}
}