First Commit

This commit is contained in:
Wesley Hofman
2025-08-29 18:13:03 +02:00
commit ecb076ce8b
39 changed files with 6982 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace WindowsFormsApp5
{
public static class DataTableHelper
{
public static DataTable ToDataTable<T>(List<T> items)
{
var dataTable = new DataTable(typeof(T).Name);
// Get all properties of the class
PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
// Create DataTable columns based on properties
foreach (var prop in properties)
{
dataTable.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
// Add rows to DataTable
foreach (var item in items)
{
var values = new object[properties.Length];
for (int i = 0; i < properties.Length; i++)
{
values[i] = properties[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
}
}