Skip to main content

Answered Question store Two Diamantion array in Datagrid SilverlightRSS Feed

(0)

bhalaniabhi
bhalaniabhi

Member

Member

47 points

25 Posts

store Two Diamantion array in Datagrid Silverlight

I have to store Two Diamantion in Datagrid ??

how it possible as a ItemSource in DataGrid in Silverlight page.xaml.cs File Smile 

example: strArray[ 5,6 ]  Stored in Datagrid as ItemSource.

Abhi Bhalani.
Associate WebDeveloper in Asp.net & Silverlight 2.0.

Allen Chen – MSFT
Allen Ch...

Star

Star

13875 points

1,804 Posts

Answered Question

Re: store Two Diamantion array in Datagrid Silverlight

Hi,

Due to current limitation of DataGrid it's not easy to do so. You can try following code:

 

xaml.cs:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Reflection.Emit;

using System.Reflection;

namespace SilverlightApplication1

{

public partial class Page : UserControl

{

public Page()

{

InitializeComponent();

this.Loaded += new RoutedEventHandler(Page_Loaded);

}

static Random r = new Random();void Page_Loaded(object sender, RoutedEventArgs e)

{

//init array

int[,] array = new int[5, 7];for (int i = 0; i < 5; i++)

{

for (int j = 0; j < 7; j++)

{

array[i, j] = r.Next(1, 100);

}

}

this.DataGrid1.ItemsSource = new DataSource_TwoDimensionArray<int>(array);

}

}

public class DataSource_TwoDimensionArray<T> : List<object>

{

private Type finished;

private int counter=0;

private void Emit(T[,] array)

{

AppDomain myDomain = AppDomain.CurrentDomain;

AssemblyName myAsmName = new AssemblyName("MyAssembly");

AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.Run);

ModuleBuilder myModule = myAssembly.DefineDynamicModule(myAsmName.Name);

TypeBuilder myType = myModule.DefineType("DataSource", TypeAttributes.Public);

 

for (int j = 0; j <= array.GetUpperBound(1); j++)

{

Type properyType = typeof(T);

FieldBuilder exField = myType.DefineField("_" + "columnName" + counter, properyType, FieldAttributes.Private);

PropertyBuilder exProperty = myType.DefineProperty("columnName" + counter, PropertyAttributes.None, properyType, Type.EmptyTypes);

//Get

MethodBuilder exGetMethod = myType.DefineMethod("get_" + "columnName"+counter, MethodAttributes.Public, properyType, Type.EmptyTypes); ILGenerator getIlgen = exGetMethod.GetILGenerator();

//IL for a simple getter:

//ldarg.0

//ldfld int32 SilverlightClassLibrary1.Class1::_Age

//ret

getIlgen.Emit(OpCodes.Ldarg_0);

getIlgen.Emit(OpCodes.Ldfld, exField);

getIlgen.Emit(OpCodes.Ret);

exProperty.SetGetMethod(exGetMethod);

//Set

MethodBuilder exSetMethod = myType.DefineMethod("set_" + "columnName" + counter, MethodAttributes.Public, null, new Type[] { properyType }); ILGenerator setIlgen = exSetMethod.GetILGenerator();

//IL for a simple setter:

//ldarg.0

//ldarg.1

//stfld int32 SilverlightClassLibrary1.Class1::_Age

//ret

setIlgen.Emit(OpCodes.Ldarg_0);

setIlgen.Emit(OpCodes.Ldarg_1);

setIlgen.Emit(OpCodes.Stfld, exField);setIlgen.Emit(OpCodes.Ret);

exProperty.SetSetMethod(exSetMethod);

//counter++

counter++;

 

}

 

finished = myType.CreateType();

 

}

 

public DataSource_TwoDimensionArray(T[,] array)

{

Emit(array);

AddPrivate(array);

}

private void AddPrivate(T[,] array)

{

 

int _count = 0;for (int i = 0; i<= array.GetUpperBound(0); i++)

{

 

object item = Activator.CreateInstance(finished);for (int j = 0; j <= array.GetUpperBound(1); j++)

{

string propertyname = "columnName" + _count;

MethodInfo method = finished.GetMethod("set_" + propertyname);method.Invoke(item, new object[] { array[i,j] });

_count++;

}

_count = 0;

base.Add(item);

}

 

}

 

}

}

xaml:

<data:DataGrid x:Name="DataGrid1"></data:DataGrid>

Sincerely,
Allen Chen
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.

timiil
timiil

Member

Member

7 points

11 Posts

Re: store Two Diamantion array in Datagrid Silverlight

Hello Mr Chen, do we have any way that use the Emit to create an SilverLight compatible assembly like your above code, BUT use common .NET platform, for saying, in normal Winform or NTService or anything else? i had decomplie the common .NET class and the Silverlight class using ILDasm, i can't see any diffrent assmebly attribute had been use...

  • Unanswered Question
  • Answered Question
  • Announcement