I've got an Ultragrid that displays all Products. Products have a Customer Foreign key (which in my case is an entire Customer Object because i use NHibernate but it serves the same purpose). Now when i bind the Products to my WinGrid it displays the type of Object in the Customer Colum (Logic.Customer). When not using NHibernate i guess it will show an integer repesenting the foreign key.
Now obviously this is not what i want. What i want is the name from the customer not the foreign key. How do i set a different display value?
Hi,
As I understand it there is no direct support for this situation but I have used the following work around on many occasions.
If you create a Custom Format Provider you can extract whatever information you need from your Customer object. To create a custom format provider use a class similar to this:
public class CustomerNameFormatProvider : IFormatProvider, ICustomFormatter
{
#region IFormatProvider Members
public object GetFormat(Type formatType)
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
#endregion
#region ICustomFormatter Members
public string Format(string format, object arg, IFormatProvider formatProvider)
try
if (arg.GetType() != typeof(Customer))
return "Unexpected data type"; // The argument is not of the expected type so return a message.
Customer cust = arg as Customer;
if (cust == null)
return string.Empty; // If the Customer is null return an empty string.
return cust.CompanyName; // Return the required property from the Customer object.
catch (Exception ex)
return ex.Message;
Then you need to set the column during initialisation to use the provider with a line similar to this:
ultraGrid1.DisplayLayout.Bands[0].Columns[15].FormatInfo = new CustomerNameFormatProvider();
Hope this helps,
Andy.
Thanks for your reply Andy.However i fixed it myself already. I made an extra unbound column and hide the existing Customer column. Then i fill it with the following code during the initialisation:
foreach (UltraGridRow rw in dgvOverview.Rows) { rw.Cells["CustomerName"].SetValue(((Customer)rw.Cells["Customer"].Value).Name, false); }
This solution feels kinda quick and dirty so i started using your solution.