How to display data from a related table for a column

3 posts, 0 answers
  1. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 22 Dec 2009 Link to this post

    This seems to be a simple solution, but I cannot find it.  I am using LinqToSQL DomainService and I have with a simple table with a column that points to a look-up table.  When I define the columns for the GridView, I tried binding to Address.StateName and nothing displays.  Here is an example of what I need...
    Example:
    Table:
    Address                
    ========
    Address1
    Address2
    City
    State
    Zip

    Table:
    States
    =======
    StateCode
    StateName

    In my gridview, I would like to display the StateName, not the StateCode

    Thanks.
  2. Karlkim Suwanmongkol
    Karlkim Suwanmongkol avatar
    41 posts
    Member since:
    Aug 2012

    Posted 23 Dec 2009 Link to this post

    I had a similar problem. And what I did was to write the ValueConverter for that column.




    public class StateCodeToNameConverter : IValueConverter 
        { 
            #region IValueConverter Members 
     
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
            { 
                if (value == null
                    return null
     
                if(value.GetType() != typeof(string)) 
                    throw new ArgumentException();             
     
                if (t != null){ 
                    // find the full state name and return; 
                 
                return null;             
            } 
     
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
            { 
                throw new NotImplementedException(); 
            } 
            #endregion 
        } 

  3. DevCraft banner
  4. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 23 Dec 2009 Link to this post

    Thank you Karlkim for your suggestion.
    I actually found out what the problem was.  Since I am using Linq to SQL DomainService, as default does not return the related entities.  I found out I had to modify the DomainService metadata and where I set up the data context. 
    To the DomainServiceMetadata, I added the [Include] attribute on the enteties I wanted returned
    DomainServiceMetadata.cs:
    // The MetadataTypeAttribute identifies RTNType_LUMetadata as the class  
        // that carries additional metadata for the RTNType_LU class.  
        [MetadataTypeAttribute(typeof(RTNType_LU.RTNType_LUMetadata))]  
        public partial class RTNType_LU  
        {  
     
            // This class allows you to attach custom attributes to properties  
            // of the RTNType_LU class.  
            //  
            // For example, the following marks the Xyz property as a  
            // required field and specifies the format for valid values:  
            //    [Required]  
            //    [RegularExpression("[A-Z][A-Za-z0-9]*")]  
            //    [StringLength(32)]  
            //    public string Xyz;  
            internal sealed class RTNType_LUMetadata  
            {  
     
                // Metadata classes are not meant to be instantiated.  
                private RTNType_LUMetadata()  
                {  
                }  
     
                public string Description;  
     
                public int ID;  
                [Include]  
                public EntitySet<RTN> RTNs;  
            }  
        } 

    Then on the Context call, I added this LoadOptions:
    DomainService.cs:
    public IQueryable<RTN> GetRTNs()  
            {  
                //In your DataContext Setup you need to specify the load options:  
                System.Data.Linq.DataLoadOptions options = new System.Data.Linq.DataLoadOptions();  
                options.LoadWith<RTN>(p => p.RTN_Marketings);  
                options.LoadWith<RTN>(R => R.RTNType_LU);  
                Context.LoadOptions = options;  
                return this.Context.RTNs;  
            } 

    This LoadOptions is only for Linq To Sql, but for Entity Framework, it is something like:
    public Person GetPersonById(long Id)  
    {  
        return this.Context.People.Include("Address").Where(p => p.Id == Id).FirstOrDefault();  

    So now, my Net Ria calls are returning the associated elements and now I can do this on my RadGridView Columns defenintion:
    <telerikGridView:RadGridView.Columns> 
         <telerikGridView:GridViewDataColumn Header="RTN" DataMemberBinding="{Binding Type_LU.Description}" /> 
    </telerikGridView:RadGridView.Columns> 

Back to Top