Bonsoir,
Voilà plusieurs semaines que je galère à chercher sur les forums (dont celui-ci) sans jamais trouver une solution complète.
Je développe une application C# Winforms avec NHibernate.
Dans ce projet, j'essaye d'afficher dans un datagridview le résultat d'une simple sélection avec NHibernate du style:
(List<Client>)_session.CreateCriteria(typeof(Client)).List<Client>();
Mon premier problème était que le datagridview n'était pas triable (normal, il fallait implémenter l'interface IBindingList). J'ai donc utilisé ceci:
link
2ème problème:
Tout marchait bien tant que j'affichais les données d'un objet pas celle d'un objet imbriqué. Par exemple le nom de rue du client:
1 2 3 4 5 6 7 8 9 10 11 12 13
| class Client
{
...
Adresse adrs;
...
}
class Adresse
{
...
String rue;
...
} |
Impossible d'afficher dans le datagridview quelque chose du style adrs.rue dans DataPropertyName des colonnes du datagridview (en supposant que j'ai une propriété me le permettant dans la class Client)
==> résolu en utilisant l'évènement CellFormatting + Reflection:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| if ((dgv.Rows[e.RowIndex].DataBoundItem != null) &&
(dgv.Columns[e.ColumnIndex].DataPropertyName.Contains(".")))
{
e.Value = BindProperty(
dgv.Rows[e.RowIndex].DataBoundItem,
dgv.Columns[e.ColumnIndex].DataPropertyName);
}
public string BindProperty(object property, string propertyName)
{
String retValue = "";
if (propertyName.Contains("."))
{
PropertyInfo[] arrayProperties;
String leftPropertyName;
leftPropertyName = propertyName.Substring(0, propertyName.IndexOf("."));
arrayProperties = property.GetType().GetProperties();
foreach (PropertyInfo propertyInfo in arrayProperties)
{
if (propertyInfo.Name == leftPropertyName)
{
retValue = BindProperty(
propertyInfo.GetValue(property, null),
propertyName.Substring(propertyName.IndexOf(".") + 1));
break;
}
}
}
else
{
Type propertyType;
PropertyInfo propertyInfo;
propertyType = property.GetType();
propertyInfo = propertyType.GetProperty(propertyName);
retValue = propertyInfo.GetValue(property, null).ToString();
}
return retValue;
} |
MAIS apparaît alors le dernier problème: il n'est pas possible de trier cette colonne en cliquant sur le header de la colonne !
Quelqu'un aurait une idée ?
J'avoue avoir déjà beaucoup cherché pour mettre en place de manière propre NHibernate avec une application WinForms que je commence à douter qu'il existe des utilisations professionnelles dans ce cadre.
Merci d'avoir été attentif jusqu'au bout
Edit: j'ajoute que j'aimerais ne pas devoir passer par un datatable
Partager