Skip to main content

Microsoft Silverlight

Binding Collection Class from Business Layer to a Control in Presentation LayerRSS Feed

(0)

silverlight programmer
silverli...

Member

Member

0 points

5 Posts

Binding Collection Class from Business Layer to a Control in Presentation Layer

Hi,

I have to bind the collection class to my datagrid, but my datagrid is in Presentation Layer, I am calling the WCF service in Business Layer, how do I do this..?

MyServiceClient proxy= new MyServiceClient ();proxy.GetData += new EventHandler<GetDataCompletedEventArgs>(proxy_GetDataCompleted);

proxy.GetDataAsync();

void proxy_GetDataCompleted(object sender, GetDataCompletedEventArgs e)

{

//--e.Result;

 //--- this is my result, how do I bind this result to my datagrid. If I call the service from Presentation layer, I can directly bind the result to datagrid as below:

mydatagrid.ItemSource=e.Result;

}

Need some help on this.

Thanks in Advance.

gv_pradeep
gv_pradeep

Member

Member

28 points

25 Posts

Re: Binding Collection Class from Business Layer to a Control in Presentation Layer

 Hi,

I guess there are many ways to achieve this. 

You can define the Event Handler method in your presentation layer and hook up the event to the event handler.

proxy.GetData + = new EventHandler<GetDataCompletedEventArgs>(presentationLayer.proxy_GetDataCompleted)

In this method, you can then directly bind the result to your datagrid.

 

silverlight programmer
silverli...

Member

Member

0 points

5 Posts

Re: Binding Collection Class from Business Layer to a Control in Presentation Layer

Hi,

Thanks for your reply.

I cannot define the Event handler in Presentation Layer because I have to consume the WCF service in my Business Layer. I have to make a call to database from my business layer only, so I want to know how to return the Collection class to my presentation layer from Business layer in order to bind to my control.

Any ideas please..?

 

 

 

kranthi.gullapalli
kranthi....

Member

Member

237 points

42 Posts

Re: Binding Collection Class from Business Layer to a Control in Presentation Layer

What you are returning from business layer?

I mean e.Result is of which Type, is it in xml format or you are passing objects from business layer to presentation layer

 

If e.Result type is xml format then you can convert it into object and bind it to datagrid as shown below:

List<ItemType> items = new List<ItemType>();

XmlReader xr = System.Xml.XmlReader.Create(new StringReader(e.Result));System.Xml.Serialization.XmlSerializer xmlSerializer= new XmlSerializer(typeof(List<ItemType>));

items = (List<ItemType>)xmlSerializer.Deserialize(xr);

now you can bind the items to the datagrid

-Thanks
Kranthi

silverlight programmer
silverli...

Member

Member

0 points

5 Posts

Re: Binding Collection Class from Business Layer to a Control in Presentation Layer

Thanks for your reply.

I am returning the Generic collection class from business layer, I mean e.result returns the collection class. The following code I perform in business layer, but proxy_GetDataCompleted method below is void, cannot return anything, how can I pass the collection class to my presentation control. It doesn't matter me whether e.result is an XML or collection class, my problem is I am unable to pass the output collection class from my business layer to presentation layer.

MyServiceClient proxy= new MyServiceClient ();

proxy.GetData += new EventHandler<GetDataCompletedEventArgs>(proxy_GetDataCompleted);

proxy.GetDataAsync();

void proxy_GetDataCompleted(object sender, GetDataCompletedEventArgs e)

{

//--e.Result; I get the result here, but I cannot pass the result to presentation layer here, since this is void.

}

Amanda Wang - MSFT
Amanda W...

All-Star

All-Star

17241 points

1,466 Posts

Re: Binding Collection Class from Business Layer to a Control in Presentation Layer

 Hi  silverlight programmer,

We would suggest that you can try to convert the e.results into an ObservableCollection, so you can try to exposed this ObservableCollection object and then you can access it in the presentation layer:

for example:

public ObservableCollection data;
void proxy_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
{
//--e.Result; I get the result here, but I cannot pass the result to presentation layer here, since this is void.
    foreach (var person in e.Result)
    {
        data.Add(person);
    }
}

Amanda Wang
Microsoft Online Community Support

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

silverlight programmer
silverli...

Member

Member

0 points

5 Posts

Re: Binding Collection Class from Business Layer to a Control in Presentation Layer

Hi Amanda,

Thanks for your reply, I have tried this way before I post this query here, but was not successful.


public ObservableCollection<MyClass> myresult= new ObservableCollection<MyClass>();

//--Calling the Service here
Public void callService()
{
    MyServiceClient proxy= new MyServiceClient ();

    proxy.GetData += new EventHandler<GetDataCompletedEventArgs>(proxy_GetDataCompleted);

    proxy.GetDataAsync();
  
    1. After this line gets executed, then it jumps directly from where the call has been made and the collection classwhich I declared globally returns null/nothing at this point of time, after this only proxy_GetDataCompleted() method gets executed, but I cannot pass the collection class results to my presentation layer.

    2. In short, I am not able to assign the results to the collection class which is declared globally to pass the results to presentation layer.

      
    3. //-- myresult.Add(new MyClass() { FirstName= "XXXX", LastName = YYYY});  //-- This line of code is for testing purpose, I have checked whether the values assigned to my collection class holds results or not, it holds the results here.

}

void proxy_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
{
    foreach (var res in e.Result)
    {
        myresult.Add(new MyClass() { FirstName = res.FirstName, LastName = res.LastName });
    }

}

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities