Getting Error in DataGridRefreshItemsSource

Sep 21, 2010 at 4:26 PM

Dear bykinag,

                                 I am getting an error while using the latest code in DataGridRefreshItemsSource where dg variable is showing null and so throwing error, attaching below is the xaml code I have used the includefieldlist and DomainEntityFieldCollection and have set the DomainDataSource properly , is there anything else I have to do to make it working.

 

<UserControl xmlns:my1="clr-namespace:DataGridProj"  
    xmlns:my="clr-namespace:DevExpress.AgDataGrid;assembly=DevExpress.AgDataGrid.v8.2"  
    x:Class="DataGridProj.MainPage"
    xmlns:riaControls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.DomainServices"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="671" d:DesignWidth="628" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:my3="clr-namespace:DataGridProj.Web"
     xmlns:primitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data" 
     xmlns:FilterControl="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data.RIADataFilter.Control"   
     xmlns:core="clr-namespace:System;assembly=mscorlib"
        >
    <UserControl.Resources>
        <core:String x:Key="includeFieldList">Countrycode,countryname</core:String>

        <FilterControl:DomainEntityFieldCollection x:Key="DomainFieldCollection">
            <FilterControl:DomainEntityField PropertyPath="Countrycode" DisplayName="Country Code"/>
            <FilterControl:DomainEntityField PropertyPath="countryname" DisplayName="Country Name"/>
        </FilterControl:DomainEntityFieldCollection>

    </UserControl.Resources>


    <Grid x:Name="LayoutRoot" Background="White" Height="646" Width="621">
        <riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my3:CountryMaster, CreateList=true}" Height="0" LoadedData="countryMasterDomainDataSource_LoadedData_4" Name="countryMasterDomainDataSource" QueryName="GetCountryMastersQuery" Width="0">
            <riaControls:DomainDataSource.DomainContext>
                <my3:DomainService1 />
            </riaControls:DomainDataSource.DomainContext>
        </riaControls:DomainDataSource>
        <sdk:DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" ItemsSource="{Binding ElementName=countryMasterDomainDataSource, Path=Data}" Margin="12,52,0,0" Name="countryMasterDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="570">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn x:Name="countryCodeColumn" Binding="{Binding Path=CountryCode}" Header="Country Code" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="countryIdColumn" Binding="{Binding Path=CountryId, Mode=OneWay}" Header="Country Id" IsReadOnly="True" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="countryNameColumn" Binding="{Binding Path=CountryName}" Header="Country Name" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="iSDCodeColumn" Binding="{Binding Path=ISDCode}" Header="ISDCode" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="regionIdColumn" Binding="{Binding Path=RegionId}" Header="Region Id" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="regionIdColumn2" Binding="{Binding Path=RegionMaster.RegionName}" Header="Region Name" Width="SizeToHeader" />
                <sdk:DataGridTextColumn x:Name="timeZoneIdColumn" Binding="{Binding Path=TimeZoneId}" Header="Time Zone Id" Width="SizeToHeader" />
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
        <!--<my1:SLDataFilterControl AllowSorting="True" AllowFiltering="True" AllowGrouping="True" x:Name="SLDataGrid" Margin="150,-33,259,464"></my1:SLDataFilterControl>-->
        <FilterControl:DataFilter Name="fltcontrol" DomainDataSource="{Binding ElementName=countryMasterDomainDataSource}" Margin="123,275,231,234">
            
        </FilterControl:DataFilter>
    </Grid>
</UserControl>

Coordinator
Sep 21, 2010 at 4:59 PM

can you please share you project with small functionality, for i can reproduce it?

Sep 23, 2010 at 7:06 AM

I have no problem sending u my project but I have used DB tables to bind the data with the filter control , can you bind a DB table to your filter and check wether it works

or not ?

 

Thanks

Ahmed Kapasi

Coordinator
Sep 23, 2010 at 7:23 AM

Ahmed,

if I had control does not work and drew this error, I would have necessarily fixed, but I haven't this error, it appears only you.

so you need to create a small test project that I was able to reproduce the error.

thanks.

Sep 23, 2010 at 8:16 AM
Edited Sep 23, 2010 at 8:21 AM

Dear bykinag,

                            I think this problem is coming due to filteroperators are not getting loaded properly do I need to do something addtional for the

for the changes you have done displaying the Field Type oriented operators. Also in one place you have hardcoded "Query" word which I found while debugging the code and

I changed my GetCountryMasters Method in my Domainservice class to GetCountryMastersQuery and then it fetched the domainentityfieldcollection

so I was wondering is there any specific changes I have to do in my project to make these new changes work in my project.

 

Thanks

Ahmed Kapasi

              

Coordinator
Sep 23, 2010 at 8:21 AM

i can't help, because i can't reproduce this error.

Sep 23, 2010 at 10:02 AM

I have sent you the project on your gmail account

Coordinator
Sep 23, 2010 at 10:03 AM

ok, i got it, analising

Sep 23, 2010 at 12:40 PM
Edited Sep 23, 2010 at 12:41 PM

Dear bykinag

did u find any solution ??

 

Thanks

Ahmed Kapasi

Coordinator
Sep 23, 2010 at 2:16 PM

bug fixed,

thanks for assistance!

look at download page - there is new binary of control, and look at the source - there are fixes.

Sep 23, 2010 at 3:25 PM

Dear bykinag,

                              Buddy thanks alot for everything, dude you rock.

 

Thanks

Ahmed Kapasi

Sep 23, 2010 at 3:32 PM

Dear bykinag,

                            I wanted to hide the columns u added ignore value and Is case sensitive , can I do it  ? can it be configured to be hidden.

 

Thanks

Ahmed Kapasi

Coordinator
Sep 23, 2010 at 6:32 PM

ahmed you absolutely right, controls must have functionality for show/hide custom columns, i think how to do that.

thanks.

Sep 24, 2010 at 12:34 PM

hi bykinag,

                       There is one issue in the Related entity grouping part, I am including a table say RegionMaster and its field RegionName and when I group on RegionName

                   my datagrid shows RegionMaster.RegionName in the group level which is the propertypath of this field but is there any way you can display the displayAttribute instead of showing

                  PropertyPath , I know that the groupdescriptor class takes input as the propertypath and the same is displayed by the datagrid , but I think there has to be someway

               to customize it.

 

Thanks

Ahmed Kapasi

Coordinator
Sep 24, 2010 at 12:56 PM

if you mean that my control should affect the behavior of others, such as the DataGrid, it is not possible.

Sep 24, 2010 at 1:21 PM

hi bykinag,

                         Ok got it, anyways there are some other issues like when I added the Domainentityfield for the Related Entity it didnt work so I have added this code in the

                     Utilities.GetListOfDomainEntityFields a bit below(Commented the lines added my Name) is code for the same so if you can check and integrate the same in your version , also I want to tell you that

                   the field like includeFieldList or excludeFieldList should be kept Case Insensitive so that programmer dont have to remember exact field name

 for that I have made changes in IsEntityFieldCanAdded also sending the code

                 for it also below

                

 

 

 

public static List<DomainEntityField> GetListOfDomainEntityFields(DomainDataSource dds, List<string> includeFieldList, List<string> excludeFieldList, DomainEntityFieldCollection domainEntityFieldCollection)
        {
            List<DomainEntityField> result = new List<DomainEntityField>();
            IDictionary<string, BindingMode> bindingModes = new Dictionary<string, BindingMode>();

            if (dds == null || string.IsNullOrWhiteSpace(dds.QueryName))
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debug.WriteLine("DomainDataSource is Null, next work of 'GetListOfDomainEntityFields' method can't be done, exiting.");
                }
                return result;
            }

            #region OLD CODE

            /*if (dds.DataView.CurrentItem == null)
            {
                if (!dds.DataView.IsEmpty)
                {
                    dds.DataView.MoveCurrentToFirst();
                }
                else
                {
                    return result;
                }

                if (dds.DataView.CurrentItem == null)
                {
                    return result;
                }
            }

            PropertyInfo[] propertyInfos = dds.DataView.CurrentItem.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            if (propertyInfos == null)
            {
                return result;
            }

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                if (propertyInfo.GetIndexParameters().Length > 0)
                {
                    // Don't generate anything for indexed properties.
                    continue;
                }

                DisplayAttribute displayAttribute =
                    propertyInfo.GetCustomAttributes(typeof(DisplayAttribute), true) //inherit
                    .Cast<DisplayAttribute>()
                    .FirstOrDefault();

                bool propertyGenerated = false;

                if (displayAttribute == null ||
                    !displayAttribute.GetAutoGenerateField().HasValue ||
                    displayAttribute.GetAutoGenerateField().Value == true)
                {
                    if (propertyInfo.GetSetMethod() != null)
                    {
                        bindingModes.Add(propertyInfo.Name, BindingMode.TwoWay);
                    }
                    else
                    {
                        bindingModes.Add(propertyInfo.Name, BindingMode.OneWay);
                    }

                    propertyGenerated = true;
                }

                string displayName = string.Empty;
                string propertyPath = string.Empty;
                Type propertyType = null;

                DomainEntityField domainEntityField = domainEntityFieldCollection.FirstOrDefault(r => r.PropertyPath == propertyInfo.Name);

                if (propertyGenerated)
                {
                    if (displayAttribute != null)
                    {
                        //GET Entity field name from DomainContext
                        if (domainEntityField == null)
                        {
                            displayName = displayAttribute.GetName();
                            if (string.IsNullOrEmpty(displayName))
                            {
                                displayName = displayAttribute.GetShortName();
                            }
                            propertyPath = propertyInfo.Name;
                            propertyType = propertyInfo.PropertyType;
                        }
                        else
                        {
                            displayName = domainEntityField.DisplayName;
                            domainEntityField.DisplayName = displayName;
                            domainEntityField.PropertyType = propertyInfo.PropertyType;
                            propertyPath = domainEntityField.PropertyPath;
                            propertyType = domainEntityField.PropertyType;
                        }
                    }
                    else
                    {
                        if (domainEntityField == null)
                        {
                            displayName = propertyInfo.Name;
                            propertyPath = propertyInfo.Name;
                            propertyType = propertyInfo.PropertyType;
                        }
                        else
                        {
                            displayName = domainEntityField.DisplayName;
                            domainEntityField.DisplayName = displayName;
                            domainEntityField.PropertyType = propertyInfo.PropertyType;
                            propertyPath = domainEntityField.PropertyPath;
                            propertyType = domainEntityField.PropertyType;
                        }
                    }

                    if (!string.IsNullOrEmpty(displayName) && !string.IsNullOrEmpty(propertyPath) && propertyType != null)
                    {
                        bool IsCanAdded = false;

                        if (includeFieldList.Count > 0 && includeFieldList.Contains(propertyPath) && !excludeFieldList.Contains(propertyPath))
                        {
                            IsCanAdded = true;
                        }
                        else

                            if (includeFieldList.Count == 0 && (!excludeFieldList.Contains(propertyPath) || excludeFieldList.Count == 0))
                            {
                                IsCanAdded = true;
                            }

                        if (IsCanAdded)
                        {
                            result.Add(new DomainEntityField() { PropertyPath = propertyPath, DisplayName = displayName, PropertyType = propertyType });
                        }
                    }
                }
            }*/

            #endregion

            string queryName = (dds.QueryName.Contains("Query")) ? dds.QueryName : string.Format("{0}Query", dds.QueryName);

            Type domainContextType = dds.DomainContext.GetType();
            MethodInfo operationInfo = domainContextType.GetMethods().Where(m => (m.Name == queryName)).FirstOrDefault();

            if (operationInfo == null)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debug.WriteLine(string.Format("DomainContext is not have QueryMethod '{0}'.", queryName));
                }
                return result;
            }

            EntityQuery query = (EntityQuery)operationInfo.Invoke(dds.DomainContext, null);
            List<PropertyInfo> propertyInfos = query.EntityType.GetEntityProperties();

            if (propertyInfos == null)
            {
                if (System.Diagnostics.Debugger.IsAttached)
                {
                    System.Diagnostics.Debug.WriteLine("EntityType is not have properties.");
                }
                return result;
            }

            if (System.Diagnostics.Debugger.IsAttached)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("Entity: '{0}'", query.EntityType.Name));
            }

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                if (propertyInfo.GetIndexParameters().Length > 0)
                {
                    // Don't generate anything for indexed properties.
                    continue;
                }

                DisplayAttribute displayAttribute = GetPropertyDisplayAttribute(query.EntityType.Name, propertyInfo, bindingModes);

                string displayName = string.Empty;
                string propertyPath = string.Empty;
                Type propertyType = null;

                

                //if RelatedEntity then Parse that, else that is ParentEntity field 
                if (propertyInfo.PropertyType.BaseType == typeof(Entity))
                {
                    string relatedEntityName = propertyInfo.Name; //PropertyName
                    string relatedEntityTypeName = propertyInfo.PropertyType.Name; //PropertyTypeName //(propertyInfo.PropertyType.FullName.Contains('.')) ? propertyInfo.PropertyType.FullName.Split('.').LastOrDefault() : string.Empty;
                    if (string.IsNullOrWhiteSpace(relatedEntityTypeName))
                    {
                        if (System.Diagnostics.Debugger.IsAttached)
                        {
                            System.Diagnostics.Debug.WriteLine(string.Format("Cant get RelatedEntity TypeName of Entity '{0}'", propertyInfo.Name));
                        }
                    }
                    else
                    {
                        if (System.Diagnostics.Debugger.IsAttached)
                        {
                            System.Diagnostics.Debug.WriteLine(string.Format("RelatedEntity: Name='{0}', TypeName='{1}'", relatedEntityName, relatedEntityTypeName));
                        }

                        foreach (var relatedEntityField in propertyInfo.PropertyType.GetEntityProperties())
                        {


                            //DomainEntityField domainEntityField = domainEntityFieldCollection.FirstOrDefault(r => r.PropertyPath == propertyInfo.Name);//Commented By Ahmed
                            DomainEntityField domainEntityField = domainEntityFieldCollection.FirstOrDefault(r => r.PropertyPath.ToLower() == relatedEntityField.Name.ToLower());
                            DisplayAttribute entityDisplayAttribute = GetPropertyDisplayAttribute(relatedEntityTypeName, relatedEntityField, bindingModes);
                            GetEntityFieldInfo(relatedEntityField, domainEntityField, entityDisplayAttribute, out displayName, out propertyPath, out propertyType);
                            string relatedPropertyPath = string.Format("{0}.{1}", relatedEntityName, propertyPath);

                            string debugMessage = string.Format("RelatedEntityField: displayName='{0}', propertyPath='{1}', propertyType='{2}'", displayName, relatedPropertyPath, propertyType.Name);

                            if (IsEntityFieldCanAdded(displayName, relatedPropertyPath, includeFieldList, excludeFieldList))
                            {
                                debugMessage += " IsAdded";
                                result.Add(new DomainEntityField() { PropertyPath = relatedPropertyPath, DisplayName = displayName, PropertyType = propertyType });
                            }

                            if (System.Diagnostics.Debugger.IsAttached)
                            {
                                System.Diagnostics.Debug.WriteLine(debugMessage);
                            }
                        }
                    }
                }
                else
                {
                    //DomainEntityField domainEntityField = domainEntityFieldCollection.FirstOrDefault(r => r.PropertyPath == propertyInfo.Name);//Commented By Ahmed
                    DomainEntityField domainEntityField = domainEntityFieldCollection.FirstOrDefault(r => r.PropertyPath.ToLower() == propertyInfo.Name.ToLower());
                    GetEntityFieldInfo(propertyInfo, domainEntityField, displayAttribute, out displayName, out propertyPath, out propertyType);

                    string debugMessage = string.Format("EntityField: displayName='{0}', propertyPath='{1}', propertyType='{2}'", displayName, propertyPath, propertyType.Name);

                    if (IsEntityFieldCanAdded(displayName, propertyPath, includeFieldList, excludeFieldList))
                    {
                        debugMessage += " IsAdded";
                        result.Add(new DomainEntityField() { PropertyPath = propertyPath, DisplayName = displayName, PropertyType = propertyType });
                    }

                    if (System.Diagnostics.Debugger.IsAttached)
                    {
                        System.Diagnostics.Debug.WriteLine(debugMessage);
                    }
                }
            }

            return result;
        }
public static bool IsEntityFieldCanAdded(string displayName, string propertyPath, List<string> includeFieldList, List<string> excludeFieldList)
        {
            
            if ((includeFieldList == null || includeFieldList.Count == 0) && (excludeFieldList == null || excludeFieldList.Count == 0))
            {
                return true;
            }
            propertyPath = propertyPath.ToLower(); //Added by Ahmed
            if (string.IsNullOrWhiteSpace(displayName) || string.IsNullOrWhiteSpace(propertyPath))
            {
                return false;
            }

            //string relatedAll = (propertyPath.Contains('.')) ? string.Format("{0}.*", propertyPath.Split('.').FirstOrDefault()) : propertyPath; //Commented By Ahmed
            string relatedAll = (propertyPath.Contains('.')) ? string.Format("{0}", propertyPath.Split('.').LastOrDefault()) : propertyPath;
            if (includeFieldList.Count > 0 && (includeFieldList.Contains(propertyPath) || includeFieldList.Contains(relatedAll))/* && !excludeFieldList.Contains(propertyPath)*/)
            {
                return true;
            }

            else if (/*!includeFieldList.Contains(propertyPath) && !includeFieldList.Contains(relatedAll) &&*/ (excludeFieldList.Count == 0 || (!excludeFieldList.Contains(propertyPath) && !excludeFieldList.Contains(relatedAll))))
            {
                return true;
            }


            return false;
        }
Coordinator
Sep 24, 2010 at 1:55 PM

ahmed thanks for your help, but can you please at the future use mechanism of this site 'Upload Patch' at the 'Source code' page?

and next, you commented one line, but not check what happens in it:

string relatedAll = (propertyPath.Contains('.')) ? string.Format("{0}.*", propertyPath.Split('.').FirstOrDefault()) : propertyPath; //Commented By Ahmed

check the logic of code, before you make modifications.

Sep 24, 2010 at 2:11 PM

Thanks for your reply , I will try to use Upload Patch in future.

 I have also added a new line below the commented line see my code as I understand each and every line of code is important.

string relatedAll = (propertyPath.Contains('.')) ? string.Format("{0}", propertyPath.Split('.').LastOrDefault()) : propertyPath;

I have done this coz I was getting problem in related field items, where I have a related field RegionMaster.RegionName and due to the previous code I was

getting the value "RegionMaster.*"  in relatedAll variable and which was not working so I changed the code a bit to correct it, I hope I have done right

 

Thanks

Ahmed Kapasi

Coordinator
Sep 24, 2010 at 4:45 PM

case insensitive of fields implemented.

Coordinator
Sep 24, 2010 at 4:52 PM
Edited Sep 24, 2010 at 4:53 PM

I think "DomainEntityFieldCollection" should be renamed to more than a name on something is "UserFrendlyEntityFieldCollection" or "UserDefinedEntityFieldCollection"

what you think?

p.s. at the last update i forgot implement case insensitive the DomainEntityFieldCollection of fields :)

Sep 30, 2010 at 7:50 AM

I think DomainEntityFieldCollection is ok,