locked
how to let the user remove an item from gridview in grid app template

    Question

  • hi,

    I'm using the grid app template project in visual c++.

    I'm using it as is but I've changed the sampledatasource to the items and groups I need.

    now I want to allow the user to select an item and remove it using the bottom app bar, I've added the bar and the button and wrote this:

    void GroupedItemsPage::appBarRemove_click(Platform::Object^ sender, Platform::Object^ e)
    {
    	auto SelectedItem = safe_cast<Data::SampleDataItem^>(itemGridView->SelectedItem);
    	auto group = SelectedItem->Group;
    	group->removeItem(SelectedItem->UniqueId);
    
    	auto sampleDataGroups = Data::SampleDataSource::GetGroups("AllGroups");
    	DefaultViewModel->Insert("Groups", sampleDataGroups);
    }

    I've also implemented the removeItem function like this:

    void SampleDataSource::RemoveItem(Platform::String^ uniqueId)
    {
    	for each (auto group in _sampleDataSource->AllGroups)
    	{
    		int i = 0;
    		for each (auto item in group->Items)
    		{
    			if (item->UniqueId->Equals(uniqueId))
    			{
    				group->Items->RemoveAt(i);
    				return ;
    			}
    			i++;
    		}
    	}
    }

    but when I click the button, the item is still there. when I debug it I can see in the group that the item is being removed but I guess I need to do something else to refresh the gridview UI to show the updated source


    Sunday, August 19, 2012 8:39 AM

All replies

  • Hello,

    You can use data binding for update the control content.
    Please follow the Scenario7 in this sample code
    http://code.msdn.microsoft.com/windowsapps/Data-Binding-7b1d67b5

    Best regards,
    Jesse

     


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, August 20, 2012 10:58 AM
  • ok, when I use the built in RemoveAt like in the sample it is working, the problem is I have a vector of groups which have inside the a vector of items (I'm using the grid app template) and I need to use my custom RemoveItem function (see above) which means I need to notify the AllGroups vector that I've removed an item so he can notify the gridview that it should be updated. how can I do that?
    Monday, August 20, 2012 12:08 PM
  • Hello,

    Your implementation seems a bit complex. Can you put a sample VC project on your Skydrive account that reproduces the issue? I'll pull it down, take a look and try to see if I can figure out what is happening.

    Thanks,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Friday, August 24, 2012 12:12 AM
    Moderator
  • ok, I really didn't do anything special, you can get the grid app template from the VS, go to the groupedItemsPage, and add a button. now in the button click handler I want it to remove the selected item in the gridview. let's say I want to remove the first item, if I'm using AllGroups->removeAt(0) it will remove the first group and not the first item.

    right now the workaround I've found is getting the group of the item, removing the item from the group and using AllGroups->SetAt(i,group), but I guess there is a better solution for this...

    Friday, August 24, 2012 6:37 AM
  • Hello,

    You said: "I want it to remove the selected item in the gridview".

    The answer really depends on how you are determining what item is selected. Personally I might change the SelectionMode to "single" and then query the selected item in the grid view and just remove the object from the grid view. Or only allow the user to remove the item from the detail page. Again it really depends on how you determine what is selected.

    I hope this helps,

    James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Friday, August 24, 2012 11:03 PM
    Moderator
  • I forgot to add that SelectionMode is set to single, I've tried to do what you said by using

    itemGridView->Items->RemoveAt(itemGridView->SelectedIndex);

    but it throws an exception. how can I do that?

    and another thing, I'm using serializing of the sampledatasource between the app runs, so my question is whould removing an item from the gridview effect the other side of the binding?


    • Edited by shmulikel Saturday, August 25, 2012 5:47 PM
    Saturday, August 25, 2012 5:47 PM
  • What is the hex code of the exception that you are getting?

    -James


    Windows Media SDK Technologies - Microsoft Developer Services - http://blogs.msdn.com/mediasdkstuff/

    Tuesday, August 28, 2012 9:09 PM
    Moderator