Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
390
Ultragrid display value
posted

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?

Parents
  • 990
    Verified Answer
    posted

    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.

                    else

                    {

                        Customer cust = arg as Customer;

                        if (cust == null)

                            return string.Empty;        // If the Customer is null return an empty string.

                        else

                            return cust.CompanyName;    // Return the required property from the Customer object.

                    }

                }

                catch (Exception ex)

                {

                    return ex.Message;

                }

            }

     

            #endregion

        } 

    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.

Reply Children
No Data