98 lines
3.3 KiB
C#
98 lines
3.3 KiB
C#
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 CreateVisualComponent
|
|
{
|
|
public partial class ListOutputComponent : UserControl
|
|
{
|
|
public ListOutputComponent()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
public int SelectedRow
|
|
{
|
|
get
|
|
{
|
|
return dataGridViewTable.SelectedRows[0].Index;
|
|
}
|
|
set
|
|
{
|
|
if (dataGridViewTable.SelectedRows.Count <= value || value < 0)
|
|
throw new ArgumentException(string.Format("{0} is an invalid row index.", value));
|
|
else
|
|
{
|
|
dataGridViewTable.ClearSelection();
|
|
dataGridViewTable.Rows[value].Selected = true;
|
|
}
|
|
}
|
|
}
|
|
public void ClearDataGrid()
|
|
{
|
|
dataGridViewTable.DataSource = null;
|
|
dataGridViewTable.Rows.Clear();
|
|
}
|
|
public void ConfigColumn(ColumnsConfiguratoin columnsData)
|
|
{
|
|
dataGridViewTable.ColumnCount = columnsData.Columns.Count;
|
|
for (int i = 0; i < columnsData.Columns.Count; i++)
|
|
{
|
|
var column = columnsData.Columns[i];
|
|
dataGridViewTable.Columns[i].Name = column.ColumnName;
|
|
dataGridViewTable.Columns[i].Width = column.Width;
|
|
dataGridViewTable.Columns[i].Visible = column.Visible;
|
|
dataGridViewTable.Columns[i].DataPropertyName = column.PropertyObject;
|
|
}
|
|
}
|
|
public T GetSelectedObjectInRow<T>() where T : class, new()
|
|
{
|
|
if (dataGridViewTable.SelectedRows.Count == 0) return null;
|
|
|
|
var val = new T();
|
|
var propertiesObj = typeof(T).GetProperties();
|
|
var selectedRow = dataGridViewTable.SelectedRows[0];
|
|
|
|
foreach (var property in propertiesObj)
|
|
{
|
|
var column = dataGridViewTable.Columns
|
|
.Cast<DataGridViewColumn>()
|
|
.FirstOrDefault(c => c.DataPropertyName == property.Name);
|
|
|
|
if (column != null)
|
|
{
|
|
var cellValue = selectedRow.Cells[column.Index].Value;
|
|
|
|
if (cellValue != DBNull.Value)
|
|
{
|
|
var convertedValue = Convert.ChangeType(cellValue, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType);
|
|
property.SetValue(val, convertedValue);
|
|
}
|
|
}
|
|
}
|
|
|
|
return val;
|
|
}
|
|
public void AddItem<T>(T item, int RowIndex, int ColumnIndex)
|
|
{
|
|
if (item == null)
|
|
return;
|
|
|
|
string propertyName = dataGridViewTable.Columns[ColumnIndex].DataPropertyName.ToString();
|
|
string? value = item.GetType().GetProperty(propertyName)?.GetValue(item)?.ToString();
|
|
|
|
if (RowIndex >= dataGridViewTable.Rows.Count)
|
|
{
|
|
dataGridViewTable.RowCount = RowIndex + 1;
|
|
}
|
|
|
|
dataGridViewTable.Rows[RowIndex].Cells[ColumnIndex].Value = value;
|
|
}
|
|
}
|
|
}
|