1

Hello I have built a WebService that returns data from SQL:

    public void ListadoWebService()
    {
       // InitializeComponent();
        ServiceTours.ServiceToursClient cl = new ServiceTours.ServiceToursClient();
        cl.ListadoCompleted += new EventHandler<ListadoCompletedEventArgs>(Listado2);
        cl.ListadoAsync();
    }
    private void Listado2(object sender, ListadoCompletedEventArgs e)
    {
        listB.ItemsSource = e.Result;
    }

Now I try to display data in columns of grid. I thought that it would work with binding the data to particular column as textblock but I can't display the data even though the data are returned in e.Result.

I tried following:

<ListBox x:Name="listB">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

    <TextBlock Text="{Binding id}" Grid.Column="0" />
    <TextBlock Text="{Binding name}" Grid.Column="1" />

                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

But still I have blackscreen.

Will somebody help me solve this out please?

Marek
  • 3,555
  • 17
  • 74
  • 123
  • I Edited my post so you'd get it, I'm going to write you a more complete answer in a minute. :) – mishan Oct 15 '13 at 15:03
  • @mishan Thank you for that. I tried to do as You and MansingDodiya suggested but I got problem with this: lst.Add(new test(Id, Name)); Got this error: PhoneApp1.test does not contain a constructor that takes 2 arguments. I don't know how to fix, may you please help? – Marek Oct 15 '13 at 15:06
  • 1
    yes, the problem is my definition does not contain contructor, while his does. Just insert `public test(string id, string name) { Id=id; Name=name; }` in the test class. :) – mishan Oct 15 '13 at 15:19
  • I will rewrite my code so it is precise for your question. I didn't write it precisely, some of the names are just what i thought them to be and not what they really are :) – mishan Oct 15 '13 at 15:23
  • @mishan The e.Result doesn't simply offer any exact names like SecondProperty. Thank you so much for your help. – Marek Oct 15 '13 at 15:24
  • So what does it offer? A list or an array of items? If you won't tell we can't help. :) Dej mi víc info :) – mishan Oct 15 '13 at 15:29
  • It does offer only when I use this: e.Result[0].Name.ToString() so I thought I can use the For loop to receive all. – Marek Oct 15 '13 at 15:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/39284/discussion-between-mishan-and-marek) – mishan Oct 15 '13 at 15:32

2 Answers2

2

first you make one getter setter method like

 public class test
    {
    public string Id{get;set;}
    public string Name{get;set;}

    public test(string id, string name)

    {
      Id=id;
      Name=name;
    }
     public test()

    {


    }

    than add your take one generc like 

    List<Test> lst=new List<test>();

    private void Listado2(object sender, ListadoCompletedEventArgs e)
        {
          lst.add(new test(id,name));
           listB.itemsource=lst;

        }

<ListBox x:Name="listB">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>

    <TextBlock Text="{Binding Id}" Grid.Column="0" />
    <TextBlock Text="{Binding Name}" Grid.Column="1" />

                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

visit below link also so you can get more idea

HOw to bind data in windows phone

How can I data bind a list of strings to a ListBox in WP7

Hope it will work for you .....

Community
  • 1
  • 1
Mansinh
  • 1,365
  • 4
  • 19
  • 47
  • Thank you for your interest helping me but I'm not sure how to implement your code so it would work. – Marek Oct 15 '13 at 14:05
  • `lst.Add(new test(Id, Name));` Got this error: PhoneApp1.test does not contain a constructor that takes 2 arguments. May I know where is the issue please? – Marek Oct 15 '13 at 15:04
  • How does your Item class look? Does it have the contructor? - the `public test(string id, string name) { Id=id; Name=name; }` method inside? - The error says: I know the class called test, but i don't have a contructor definition that says i what i should do with two string arguments. – mishan Oct 15 '13 at 15:15
  • @mishan:yes it have constructor – Mansinh Oct 16 '13 at 06:44
  • @MansinhDodiya I know your class has a constructor, I was asking him whether he added contructor to his code or not. :) – mishan Oct 16 '13 at 06:47
  • @MansinhDodiya + we already solved the problem on chat :) - he was doing it right in the end, the problem was the e.Result was some kind of collection of objects with five or more parameters. – mishan Oct 16 '13 at 06:49
1

I might be wrong here, but doesn't the e.Result cease to exist as soon as the Listado2 is over?

I'm not exactly profesional, but what I would do is I would copy the stuff from result somewhere where i could preserve it longer.

As i read the previous answer by MansinhDodiya he is basically telling you the same thing.

  1. Make a class called Item containing two Properties (the public string Id{get;set;} is a property)

    There are more ways to do it, one of them being to go to the code-behind (the .xaml.cs of that page and create the class there, other being creating a new class in the same namespace ad third, setting up new namespace and adding using).

    So in the .xaml.cs of the page create the class like that:

     class Item
     {
        public string Id {get;set;} //this is the first property i would later bind
        public string Name {get;set;} //this is the second property
    
        public Item(string id, string name)  // this is the contructor, every time
                                             // an instance of Item is created, this
                                             // method is called
        {
           Id = id;
           Name = name;
        }
     }
    
  2. Make a list of these Items somewhere accesible

    Next to that class, inside the page class, create an instance of that list:

    List<Item> itemlist = new List<Item>();
    
  3. copy the data into that List<Item> and then set that list as itemsSource of the listbox.

    Inside Listado2 copy the stuff from e.Result into the itemlist:

    private void Listado2(object sender, ListadoCompletedEventArgs e)
    {
       ...copying from e.Result into itemList...
       listB.itemsource = itemList;
    }
    
  4. And then change the xaml binding definition to the names of the properties - in my case:

    <ListBox x:Name="listB">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                ..omitted...
                   <TextBlock Text="{Binding Id}" Grid.Column="0" />
                   <TextBlock Text="{Binding Name}" Grid.Column="1" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
    

mishan
  • 1,177
  • 3
  • 19
  • 42
  • Thank so much! But when I do in `Listado2` the `Id = e.Result.Id` there is no offer for `.Id` or `.Name`. I`m not sure how to fix that. – Marek Oct 15 '13 at 15:19
  • I reworked it, omitted the copying part since i don't know how the Result looks like. – mishan Oct 15 '13 at 15:32