diff --git a/ComponentProgramming/ComponentProgramming/ControlDataGrid.Designer.cs b/ComponentProgramming/ComponentProgramming/ControlDataGrid.Designer.cs new file mode 100644 index 0000000..f532c47 --- /dev/null +++ b/ComponentProgramming/ComponentProgramming/ControlDataGrid.Designer.cs @@ -0,0 +1,65 @@ +namespace ComponentProgramming +{ + partial class ControlDataGrid + { + /// + /// Обязательная переменная конструктора. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Освободить все используемые ресурсы. + /// + /// истинно, если управляемый ресурс должен быть удален; иначе ложно. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Код, автоматически созданный конструктором компонентов + + /// + /// Требуемый метод для поддержки конструктора — не изменяйте + /// содержимое этого метода с помощью редактора кода. + /// + 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; + } +} diff --git a/ComponentProgramming/ComponentProgramming/ControlDataGrid.cs b/ComponentProgramming/ComponentProgramming/ControlDataGrid.cs new file mode 100644 index 0000000..6df932b --- /dev/null +++ b/ComponentProgramming/ComponentProgramming/ControlDataGrid.cs @@ -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 headers, List width, List isVisible, List 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() 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; + } + } +} diff --git a/ComponentProgramming/ComponentProgramming/ControlDataGrid.resx b/ComponentProgramming/ComponentProgramming/ControlDataGrid.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/ComponentProgramming/ComponentProgramming/ControlDataGrid.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/ComponentProgramming/Forms/Form.Designer.cs b/ComponentProgramming/Forms/Form.Designer.cs index 363c7d0..b290722 100644 --- a/ComponentProgramming/Forms/Form.Designer.cs +++ b/ComponentProgramming/Forms/Form.Designer.cs @@ -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; } } diff --git a/ComponentProgramming/Forms/Form.cs b/ComponentProgramming/Forms/Form.cs index 7b5cea9..0cbbd1a 100644 --- a/ComponentProgramming/Forms/Form.cs +++ b/ComponentProgramming/Forms/Form.cs @@ -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 headers = new List() { "Id", "" }; + List width = new List() { 200, 200 }; + List isVisible = new List() { true, true }; + List props = new List() { "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(); + MessageBox.Show($"{da?.Id}:{da?.Name} "); + } + + private void buttonClear_Click(object sender, EventArgs e) + { + controlDataGrid.ClearData(); + } } } diff --git a/ComponentProgramming/Forms/Person.cs b/ComponentProgramming/Forms/Person.cs new file mode 100644 index 0000000..33d312a --- /dev/null +++ b/ComponentProgramming/Forms/Person.cs @@ -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; + } + } +}