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

[Solved] Grid Export Problem

15 Answers 279 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
boris
Top achievements
Rank 1
boris asked on 27 Nov 2009, 12:23 PM
Hello,
I have a very specific problem with exporting a data from telerik grid. Our data is dynamic, so number of columns vary, and the columns are defined dynamically. Also, because there are no anonymous types in silverlight, we had to define a new type dynamically with any number of properties we want to display in the grid.
It all works fine, and the data is shown in the grid as in the picture attached, but the problem is when I try to export the data, I do not get values in cells, but some text "TempType..." I am also sending a picture of a text document with exported data.
I have noticed that we have the same problem when doing vertical scroll - the tool-tip text that appears also says "TempType".

15 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 27 Nov 2009, 12:46 PM
Hi boris,

I believe that this is already fixed in our latest internal build - please try it and let me know how it goes. Other possible option is to use my DataTable instead (a bit different dynamic types generation approach for Silverlight).

Sincerely yours,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
boris
Top achievements
Rank 1
answered on 27 Nov 2009, 01:56 PM
hi Vlad, thank You for answering.
The latest builds did not help; it still exports as "TempType"... :(
As  for DataTable implementation, I'm afraid we are in too deep with usage of dynamic types to turn to DataTable at this point :S
Any chance this will work in the future builds, or do I have to look for alternative approach?
0
Vlad
Telerik team
answered on 27 Nov 2009, 02:09 PM
Hi boris,

Can you send us small example (via support ticket) where we can reproduce and debug this? I'll gladly help you!

Regards,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
boris
Top achievements
Rank 1
answered on 27 Nov 2009, 03:38 PM
Hello Vlad,
I have created a demo solution that should explain my problem.
ticket ID is: 262484
0
boris
Top achievements
Rank 1
answered on 27 Nov 2009, 06:28 PM
Another thing I've noticed now is that when I don't use custom columns but automatically generated columns, export works fine. So it looks like it was never about dynamic types, and the problem are in fact the custom columns ( or combination of the two?) 
0
Accepted
Vlad
Telerik team
answered on 01 Dec 2009, 08:22 AM
Hi boris,

I've modified your demo to illustrate you how to achieve your goal using Exporting event in your scenario.

Please check the attached project and let me know how it goes.

Sincerely yours,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
boris
Top achievements
Rank 1
answered on 01 Dec 2009, 12:45 PM
Hi Vlad,
That is it, thank you very much for your effort! :)

0
boris
Top achievements
Rank 1
answered on 08 Dec 2009, 09:42 AM
More problems with export... :S
This apprach works fine for ToHtml() extension method, but I need to export to doc, xls and csv usint ToText(), ToExcelML() and ToCSV() extension methods. The problem is when I use these methods, this condition is never hit:
if (e.Element == ExportElement.Row)
So, I still have those "TempType" cells exported......
:(
0
Vlad
Telerik team
answered on 08 Dec 2009, 09:55 AM
Hello boris,

 If you define ToString() method for your dynamic types which will return actual values instead type name you will be able to export with other formats as well.

Regards,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
boris
Top achievements
Rank 1
answered on 08 Dec 2009, 11:17 AM
Hi Vlad,
I am not sure what should I override; every row is bound to a temp type, but inside this object the properties that are bound to columns are all strings. I also tried to debug this, and when I get the datasource generated as IEnumerable, I can read each property, and if I try to do ToString() on it, I get the desired string... only thing that gives me "TempType" response is the whole object containing these properties, but If I override that object's ToString(), than I am not getting a values for each cell?
0
boris
Top achievements
Rank 1
answered on 11 Dec 2009, 01:11 PM
Hi Vlad,
I still don't have any idea how to resolve this, maybe you can show me on the example I provided what you meant when you said I should override ToString() ?
Tnx.
0
Vlad
Telerik team
answered on 11 Dec 2009, 01:26 PM
Hello boris,

 The idea is to define ToString() method for your dynamic type which will return desired property value instead of type name. You can do this similar to your own dynamic get/set methods:

private static void CreateProperty(
                        TypeBuilder tb, string propertyName, Type propertyType)
        {
            FieldBuilder fieldBuilder = tb.DefineField("_" + propertyName,
                                                        propertyType,
                                                        FieldAttributes.Private);


            PropertyBuilder propertyBuilder =
                tb.DefineProperty(
                    propertyName, PropertyAttributes.HasDefault, propertyType, null);
            MethodBuilder getPropMthdBldr =
                tb.DefineMethod("get_" + propertyName,
                    MethodAttributes.Public |
                    MethodAttributes.SpecialName |
                    MethodAttributes.HideBySig,
                    propertyType, Type.EmptyTypes);

            ILGenerator getIL = getPropMthdBldr.GetILGenerator();

            getIL.Emit(OpCodes.Ldarg_0);
            getIL.Emit(OpCodes.Ldfld, fieldBuilder);
            getIL.Emit(OpCodes.Ret);

            MethodBuilder setPropMthdBldr =
                tb.DefineMethod("set_" + propertyName,
                  MethodAttributes.Public |
                  MethodAttributes.SpecialName |
                  MethodAttributes.HideBySig,
                  null, new Type[] { propertyType });

            ILGenerator setIL = setPropMthdBldr.GetILGenerator();

            setIL.Emit(OpCodes.Ldarg_0);
            setIL.Emit(OpCodes.Ldarg_1);
            setIL.Emit(OpCodes.Stfld, fieldBuilder);
            setIL.Emit(OpCodes.Ret);

            propertyBuilder.SetGetMethod(getPropMthdBldr);
            propertyBuilder.SetSetMethod(setPropMthdBldr);
        }

You can use Microsoft Dynamic LINQ library for reference. Here is an example with GetHashCode:

void GenerateGetHashCode(TypeBuilder tb, FieldInfo[] fields) {
            MethodBuilder mb = tb.DefineMethod("GetHashCode",
                MethodAttributes.Public | MethodAttributes.ReuseSlot |
                MethodAttributes.Virtual | MethodAttributes.HideBySig,
                typeof(int), Type.EmptyTypes);
            ILGenerator gen = mb.GetILGenerator();
            gen.Emit(OpCodes.Ldc_I4_0);
            foreach (FieldInfo field in fields) {
                Type ft = field.FieldType;
                Type ct = typeof(EqualityComparer<>).MakeGenericType(ft);
                gen.EmitCall(OpCodes.Call, ct.GetMethod("get_Default"), null);
                gen.Emit(OpCodes.Ldarg_0);
                gen.Emit(OpCodes.Ldfld, field);
                gen.EmitCall(OpCodes.Callvirt, ct.GetMethod("GetHashCode", new Type[] { ft }), null);
                gen.Emit(OpCodes.Xor);
            }
            gen.Emit(OpCodes.Ret);
        }

Regards,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
boris
Top achievements
Rank 1
answered on 14 Dec 2009, 08:18 AM
Hi Vlad, thanks for sticking with me on this one.
I understand what you are trying to say, but how can that help me?
If I override ToString() on an object that is bound to a row, this still doesn't give me individual strings for each cell in that row..
Am I missing something...? :S
0
Accepted
Vlad
Telerik team
answered on 17 Dec 2009, 09:47 AM
Hello boris,

 Actually I've found that you do not use DataMemberBinding property of these columns at all and all these problems are related to this. I've attached modified version of your project for reference.


All the best,
Vlad
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
boris
Top achievements
Rank 1
answered on 21 Dec 2009, 09:40 AM
Thanks Vlad,
That did the trick, it exports now :)
Now my next problem is applying some styling, but I'll open a new thread for that one.. this is resolved.

Thank you very much :D
Tags
GridView
Asked by
boris
Top achievements
Rank 1
Answers by
Vlad
Telerik team
boris
Top achievements
Rank 1
Share this question
or