locked
WCF DataProvider to Listview Error RRS feed

  • Question

  • I am trying to fill a list view inside a stackpanel with textboxes with data from my backend service. I can get the service to work without any problem and it pulls back the 7 rows of data I need from my database.

    The problem I am having is that when I go to put the data in the listview I keep getting an error below.

    {"Collection was modified; enumeration operation may not execute."}

    This happens at my foreach statement below. If uncomment the itemslistview line and comment out the tickets.add items, then it works but not in very nice format.

    Any ideas.

    DataProvider.DataProviderClient serviceClient = new DataProvider.DataProviderClient();
    ObservableCollection<DataProvider.Ticket> tickets = await serviceClient.getTicketsAsync();
    foreach (var lvitems in tickets)
      {
         //itemListView.Items.Add(ticket.ID + " " + ticket.UserName + " " + ticket.DeptName);
         tickets.Add(new DataProvider.Ticket
         {
           ID = lvitems.ID,
           UserName = lvitems.UserName,
           DeptName = lvitems.DeptName,
           StatusName = lvitems.StatusName
          });
      }

    Thursday, October 10, 2013 7:49 PM

Answers

  • Hi,rlawrimore

    You can create a LiseView DataTemplate in XAML and bind the tickets object to Listview Itemsource.There are some codes below you can refer to:

     <ListView Name="listview" ItemsSource="{Binding}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBox Text="{Binding ID}" Margin="0,0,10,0"></TextBox>
                            <TextBox Text="{Binding UserName}" Margin="0,0,10,0"></TextBox>
                            <TextBox Text="{Binding DeptName}" Margin="0,0,10,0"></TextBox>
                            <TextBox Text="{Binding StatusName}" Margin="0,0,10,0"></TextBox>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

    code-behind:

    DataProvider.DataProviderClient serviceClient = new DataProvider.DataProviderClient();
    ObservableCollection<DataProvider.Ticket> tickets = await serviceClient.getTicketsAsync();
     listview.DataContext = tickets;

    You can get more information in these links to know how to set binding in controls:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh758318.aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/hh464965.aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/hh758320.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Ronald Lawrimore Friday, October 11, 2013 3:26 PM
    • Marked as answer by rlawrimore Friday, October 11, 2013 3:27 PM
    Friday, October 11, 2013 7:24 AM

All replies

  • Hi,rlawrimore

    You can create a LiseView DataTemplate in XAML and bind the tickets object to Listview Itemsource.There are some codes below you can refer to:

     <ListView Name="listview" ItemsSource="{Binding}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBox Text="{Binding ID}" Margin="0,0,10,0"></TextBox>
                            <TextBox Text="{Binding UserName}" Margin="0,0,10,0"></TextBox>
                            <TextBox Text="{Binding DeptName}" Margin="0,0,10,0"></TextBox>
                            <TextBox Text="{Binding StatusName}" Margin="0,0,10,0"></TextBox>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

    code-behind:

    DataProvider.DataProviderClient serviceClient = new DataProvider.DataProviderClient();
    ObservableCollection<DataProvider.Ticket> tickets = await serviceClient.getTicketsAsync();
     listview.DataContext = tickets;

    You can get more information in these links to know how to set binding in controls:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh758318.aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/hh464965.aspx

    http://msdn.microsoft.com/en-us/library/windows/apps/hh758320.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • Proposed as answer by Ronald Lawrimore Friday, October 11, 2013 3:26 PM
    • Marked as answer by rlawrimore Friday, October 11, 2013 3:27 PM
    Friday, October 11, 2013 7:24 AM
  • that works great. Much easier than what I was doing.

    Friday, October 11, 2013 3:26 PM