跟小D每日学口语

How to Display DataSet in Silverlight DataGrid

What you want is to be able to dynamically build a DataGrid in SilverLight with ANY data you may deliver from the server.  The data could be an Array of records, a DataSet, XML, or weakly typed JSON, it doesn't matter.  Here is the code I use to do that, and also dynamically create the columns.  I found this code somewhere, but I don't remember where, but it works great.

Here is the code that creates the DataGrid:
    public partial class DynamicGrid : UserControl
    {
        public DynamicGrid()
        {
            InitializeComponent();

            Canvas.SetLeft(butt, 500);

            this.theGrid.Columns.Add(
                        new DataGridTextColumn
                        {
                            Header = "ID",
                            DisplayMemberBinding = new Binding("ID")
                        });
            this.theGrid.Columns.Add(
                        new DataGridTextColumn
                        {
                            Header = "Name",
                            DisplayMemberBinding = new Binding("Name")
                        });
            this.theGrid.Columns.Add(
                        new DataGridTextColumn
                        {
                            Header = "Index",
                            DisplayMemberBinding = new Binding("Index")
                        });
            this.theGrid.Columns.Add(
                        new DataGridCheckBoxColumn
                        {
                            Header = "Is Even",
                            DisplayMemberBinding = new Binding("IsEven")
                        });
            this.theGrid.ItemsSource = GenerateData().ToDataSource();
        }

        public IEnumerable<IDictionary> GenerateData()
        {
            for (var i = 0; i < 1000; i++)
            {
                var dict = new Dictionary<string, object>();
                dict.Add("ID", Guid.NewGuid());
                dict.Add("Name", "Name_" + i);
                dict.Add("Index", i);
                dict.Add("IsEven", i % 2 == 0);
                yield return dict;
            }
        }
    }

To make this work, you need the following Extension Method:
    public static class DataSourceCreator
    {
        private static readonly Regex PropertNameRegex =
                new Regex(@"^[A-Za-z]+[A-Za-z1-9_]*$", RegexOptions.Singleline);

        public static object[] ToDataSource(this IEnumerable<IDictionary> list)
        {
            IDictionary firstDict = null;
            bool hasData = false;
            foreach (IDictionary currentDict in list)
            {
                hasData = true;
                firstDict = currentDict;
                break;
            }
            if (!hasData)
            {
                return new object[] { };
            }
            if (firstDict == null)
            {
                throw new ArgumentException("IDictionary entry cannot be null");
            }

            Type objectType = null;

            TypeBuilder tb = GetTypeBuilder(list.GetHashCode());

            ConstructorBuilder constructor =
                        tb.DefineDefaultConstructor(
                                    MethodAttributes.Public |
                                    MethodAttributes.SpecialName |
                                    MethodAttributes.RTSpecialName);

            foreach (DictionaryEntry pair in firstDict)
            {
                if (PropertNameRegex.IsMatch(Convert.ToString(pair.Key), 0))
                {
                    CreateProperty(tb,
                                    Convert.ToString(pair.Key),
                                    pair.Value == null ?
                                                typeof(object) :
                                                pair.Value.GetType());
                }
                else
                {
                    throw new ArgumentException(
                                @"Each key of IDictionary must be
                                alphanumeric and start with character.");
                }
            }
            objectType = tb.CreateType();
            return GenerateArray(objectType, list, firstDict);
        }

        private static object[] GenerateArray(Type objectType, IEnumerable<IDictionary> list, IDictionary firstDict)
        {
            var itemsSource = new List<object>();
            foreach (var currentDict in list)
            {
                if (currentDict == null)
                {
                    throw new ArgumentException("IDictionary entry cannot be null");
                }
                object row = Activator.CreateInstance(objectType);
                foreach (DictionaryEntry pair in firstDict)
                {
                    if (currentDict.Contains(pair.Key))
                    {
                        PropertyInfo property =
                            objectType.GetProperty(Convert.ToString(pair.Key));
                        property.SetValue(
                            row,
                            Convert.ChangeType(
                                    currentDict[pair.Key],
                                    property.PropertyType,
                                    null),
                            null);
                    }
                }
                itemsSource.Add(row);
            }
            return itemsSource.ToArray();
        }

        private static TypeBuilder GetTypeBuilder(int code)
        {
            AssemblyName an = new AssemblyName("TempAssembly" + code);
            AssemblyBuilder assemblyBuilder =
                AppDomain.CurrentDomain.DefineDynamicAssembly(
                    an, AssemblyBuilderAccess.Run);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");

            TypeBuilder tb = moduleBuilder.DefineType("TempType" + code
                                , TypeAttributes.Public |
                                TypeAttributes.Class |
                                TypeAttributes.AutoClass |
                                TypeAttributes.AnsiClass |
                                TypeAttributes.BeforeFieldInit |
                                TypeAttributes.AutoLayout
                                , typeof(object));
            return tb;
        }

        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);
        }
    }

This will yield the following Grid:

posted @ 2009-02-04 20:19  javak  阅读(668)  评论(1编辑  收藏  举报