136 lines
3.7 KiB
C#
136 lines
3.7 KiB
C#
using DevExpress.Blazor;
|
|
using Microsoft.AspNetCore.Components;
|
|
using System.Reflection;
|
|
|
|
namespace AyCode.Blazor.Components.Components.Grids;
|
|
|
|
public partial class MgGridInfoPanel<TDataItem> : ComponentBase where TDataItem : class
|
|
{
|
|
private DxGrid? _currentGrid;
|
|
private TDataItem? _currentDataItem;
|
|
private List<DxGridDataColumn> _allDataColumns = [];
|
|
|
|
/// <summary>
|
|
/// Refreshes the InfoPanel with data from the specified grid row
|
|
/// </summary>
|
|
/// <param name="grid">The grid instance</param>
|
|
/// <param name="dataItem">The data item from the focused row</param>
|
|
public void RefreshData(DxGrid grid, TDataItem? dataItem)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(grid);
|
|
|
|
_currentGrid = grid;
|
|
_currentDataItem = dataItem;
|
|
|
|
if (_currentGrid != null && _currentDataItem != null)
|
|
{
|
|
_allDataColumns = GetAllDataColumns(_currentGrid);
|
|
}
|
|
else
|
|
{
|
|
_allDataColumns = [];
|
|
}
|
|
|
|
InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears the InfoPanel
|
|
/// </summary>
|
|
public void Clear()
|
|
{
|
|
_currentGrid = null;
|
|
_currentDataItem = null;
|
|
_allDataColumns = [];
|
|
InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
private List<DxGridDataColumn> GetAllDataColumns(DxGrid grid)
|
|
{
|
|
var columns = new List<DxGridDataColumn>();
|
|
|
|
try
|
|
{
|
|
var allColumns = grid.GetDataColumns();
|
|
if (allColumns != null)
|
|
{
|
|
foreach (var column in allColumns)
|
|
{
|
|
// Minden DxGridDataColumn-t felveszünk, ha van FieldName-je
|
|
// NEM vizsgáljuk a Visible property-t!
|
|
if (column is DxGridDataColumn dataColumn &&
|
|
!string.IsNullOrWhiteSpace(dataColumn.FieldName))
|
|
{
|
|
columns.Add(dataColumn);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// Fallback: empty list if GetDataColumns fails
|
|
}
|
|
|
|
return columns;
|
|
}
|
|
|
|
private object? GetCellValue(DxGridDataColumn column)
|
|
{
|
|
if (_currentDataItem == null || string.IsNullOrWhiteSpace(column.FieldName))
|
|
return null;
|
|
|
|
try
|
|
{
|
|
var fieldName = column.FieldName;
|
|
var properties = fieldName.Split('.');
|
|
object? currentValue = _currentDataItem;
|
|
|
|
foreach (var propertyName in properties)
|
|
{
|
|
if (currentValue == null)
|
|
break;
|
|
|
|
var propertyInfo = currentValue.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
|
|
if (propertyInfo == null)
|
|
return null;
|
|
|
|
currentValue = propertyInfo.GetValue(currentValue);
|
|
}
|
|
|
|
return currentValue;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private string GetDisplayText(DxGridDataColumn column, object? value)
|
|
{
|
|
if (value == null)
|
|
return string.Empty;
|
|
|
|
if (value is DateTime dateTime)
|
|
{
|
|
return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
|
|
}
|
|
|
|
if (value is bool boolValue)
|
|
{
|
|
return boolValue ? "Igen" : "Nem";
|
|
}
|
|
|
|
if (value is decimal || value is double || value is float)
|
|
{
|
|
return string.Format("{0:N2}", value);
|
|
}
|
|
|
|
if (value is int || value is long || value is short || value is byte)
|
|
{
|
|
return string.Format("{0:N0}", value);
|
|
}
|
|
|
|
return value.ToString() ?? string.Empty;
|
|
}
|
|
}
|