Третий компонент??
This commit is contained in:
parent
8771c2c962
commit
b84f61e1cb
1
VisualComponentsLib/MyDataGridView.Designer.cs
generated
1
VisualComponentsLib/MyDataGridView.Designer.cs
generated
@ -39,6 +39,7 @@
|
||||
dataGridView.Location = new Point(3, 3);
|
||||
dataGridView.Name = "dataGridView";
|
||||
dataGridView.RowTemplate.Height = 25;
|
||||
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
||||
dataGridView.Size = new Size(290, 177);
|
||||
dataGridView.TabIndex = 0;
|
||||
//
|
||||
|
@ -4,6 +4,7 @@ using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
@ -12,6 +13,28 @@ namespace VisualComponentsLib
|
||||
{
|
||||
public partial class MyDataGridView : UserControl
|
||||
{
|
||||
//метод для получения индекса выбранной строки
|
||||
public int IndexRow
|
||||
{
|
||||
get
|
||||
{
|
||||
if (dataGridView.CurrentCell == null)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return dataGridView.CurrentCell.RowIndex;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
if (dataGridView.CurrentCell != null)
|
||||
{
|
||||
dataGridView.CurrentCell = dataGridView.Rows[value].Cells[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MyDataGridView()
|
||||
{
|
||||
InitializeComponent();
|
||||
@ -51,11 +74,35 @@ namespace VisualComponentsLib
|
||||
}
|
||||
|
||||
//публичный параметризованный метод для получения нового объекта из списка
|
||||
public T GetObject<T>() where T : class, new()
|
||||
public T GetSelectedObject<T>() where T : new()
|
||||
{
|
||||
if(dataGridView.SelectedRows != )
|
||||
if (dataGridView.SelectedCells.Count == 0)
|
||||
{
|
||||
return new T();
|
||||
}
|
||||
|
||||
return null;
|
||||
int rowIndex = dataGridView.SelectedCells[0].RowIndex;
|
||||
var targetObject = new T();
|
||||
|
||||
Type objectType = typeof(T);
|
||||
|
||||
PropertyInfo[] properties = objectType.GetProperties();
|
||||
|
||||
foreach (PropertyInfo property in properties)
|
||||
{
|
||||
DataGridViewCell selectedCell = dataGridView.Rows[rowIndex].Cells[property.Name];
|
||||
|
||||
object cellValue = selectedCell.Value;
|
||||
|
||||
if (cellValue != null && property.CanWrite)
|
||||
{
|
||||
object convertedValue = Convert.ChangeType(cellValue, property.PropertyType);
|
||||
|
||||
property.SetValue(targetObject, convertedValue);
|
||||
}
|
||||
}
|
||||
|
||||
return targetObject;
|
||||
}
|
||||
|
||||
//полная очистка
|
||||
|
Loading…
x
Reference in New Issue
Block a user