This is a migrated thread and some comments may be shown as answers.

How to display data from a related table for a column

2 Answers 79 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hector
Top achievements
Rank 1
Hector asked on 22 Dec 2009, 05:30 PM
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 Answers, 1 is accepted

Sort by
0
Karlkim Suwanmongkol
Top achievements
Rank 1
answered on 23 Dec 2009, 04:39 PM
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 
    } 

0
Hector
Top achievements
Rank 1
answered on 23 Dec 2009, 04:51 PM
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> 

Tags
GridView
Asked by
Hector
Top achievements
Rank 1
Answers by
Karlkim Suwanmongkol
Top achievements
Rank 1
Hector
Top achievements
Rank 1
Share this question
or