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

Add Columns to grid

3 Answers 76 Views
GridView
This is a migrated thread and some comments may be shown as answers.
stijn Ver Eecke
Top achievements
Rank 1
stijn Ver Eecke asked on 07 Jul 2010, 04:28 PM
Hi ,
I am new to Wpf, EntityFramework and Telerik,
but i am trying to do the following :
I have a generic gridwindow ( telerik grid ) , that doesnt know what data it will display at compile time.
The grid is set to autogenerate the columns once it get a collection of data.

The data is a observable collection of EF Entities.
In the AutoGeneratingColumn event, I intercept each column.
columns of type Collection are skipped over,
columns of type String, Datetime or primitive types are shown.

I would like to replace the single columns to other Entities by n columns for their own primitive values.
So if i have a Entity Customer with a Property Address.
My grid would show by default :
Customer fields with binding path : Name, FirstName etc.
Address fields with binding path : Address.City, Address.Zip etc. all on the same row.

the code :
the Event :
 Telerik.Windows.Controls.GridViewBoundColumnBase c = gvc as Telerik.Windows.Controls.GridViewBoundColumnBase;
     if ((!c.DataType.FullName.StartsWith("Afda.Model.TrackableCollection"))
              &&  (!c.DataType.FullName.StartsWith("Afda.Model.Obj")))
                {
                    foreach (System.Reflection.PropertyInfo pi2 in c.DataType.GetProperties())
                    {
                        if (pi2.PropertyType.BaseType.IsValueType || pi2.PropertyType.FullName.Equals("System.String") || pi2.PropertyType.FullName.Equals("System.DateTime")
                            || pi2.PropertyType.BaseType.FullName.Equals("System.ValueType"))
                        {
                            Telerik.Windows.Controls.GridViewDataColumn newColumn = new Telerik.Windows.Controls.GridViewDataColumn();
                            newColumn.Width = c.ActualWidth;
                            newColumn.DisplayIndex = c.DisplayIndex;
                            String s = c.DataMemberBinding.Path.Path + "." + pi2.Name;
                            newColumn.DataMemberBinding = new Binding(s);
                            newColumn.DataType = pi2.PropertyType;
                            newColumn.DataFormatString = c.DataFormatString;
                            newColumn.TextAlignment = c.TextAlignment;
                            newColumn.Header = s;
                            newColumn.Name = s;
                            newColumn.UniqueName = s;
                            newColumn.Footer = c.Footer;
                            newColumn.IsVisible = true;
                            colsToAdd.Add(newColumn);
                        }
                    }
                    gvc.IsVisible = false;

In the load data method :
 
  try
            {
                DataGrid.ItemsSource = data;
             }
            catch { }

            foreach (Telerik.Windows.Controls.GridViewDataColumn c in colsToAdd) {
                if (!DataGrid.Columns.Contains(c)) {
                    DataGrid.Columns.Add(c);
                }
            }


PS : i also tried doing the add of the new column in the event itsself, but it seemed Icky , and gave a cryptic error.

Any comments ?? it seems to be working most of the time .  ( the embedded Entity isnt always loaded, even if the linq statment is always the same )

No real problems with it atm, so forum prob not the place to post this, but it wasnt working yet, when i started writing :)



3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 13 Jul 2010, 07:41 AM
Hi,

 Do you still have problems with this? Let me know if you need our assistance!

Kind regards,
Vlad
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
stijn Ver Eecke
Top achievements
Rank 1
answered on 22 Jul 2010, 04:10 PM
Hi,

like I said at the bottom, I dont have any real problems, just wondering if this is the best way to do it,
or if it could be done in a better way ?

greetings
Stijn
0
Milan
Telerik team
answered on 22 Jul 2010, 08:06 PM
Hello stijn Ver Eecke,

It seems okay. Maybe it is a bit complicated but I cannot think of another way that will not be as complicated as this one. 


Regards,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
stijn Ver Eecke
Top achievements
Rank 1
Answers by
Vlad
Telerik team
stijn Ver Eecke
Top achievements
Rank 1
Milan
Telerik team
Share this question
or