0

I am writing a Windows 8 app (XAML-C#-Windows Store app (metro)) where I have to loop through a GridView's SelectedItems collection.

Here is my code:

private void bottomAppBarBotonEliminar_Tapped(object sender, TappedRoutedEventArgs e)
{
    //Borrar el(los) elemento(s) seleccionado(s)
    foreach (GridViewItem elItem in GVElementos.SelectedItems)
    {
        MiColeccion.RemoveAt(GVElementos.Items.IndexOf(elItem));
    }
    ElementoSQL.Sincronizar(MiColeccion);
}

When I run it and that method is fired, I get the following error (translated from Spanish):

An exception of type 'System.InvalidCastException' occurred 
in Lista.exe but was not handled in user code
Additional information: Unable to convert an object of 
type 'System.String' to the type 'Windows.UI.Xaml.Controls.GridViewItem'.

When the program breaks, Visual Studio highlights the line with the foreach statement.

"GVElementos" is a XAML GridView.
Isn't "elItem" of type "GridViewItem" and "GVElementos.SelectedItems" a collection of elements of type "GridViewItem"?
What am I doing wrong? Is there another way to iterate the GridView? I come from ASP.NET where this way of doing it makes sense.

3
  • 1
    Have you paid attention to the message? It says the item is a string. Forget what you know from ASP.NET, you are using a different framework with different controls. Learn how to use them. First step (in this case) is recognize the error message. Next step is to consult the documentation and actually learn more about the tools you are using. Feb 24, 2013 at 4:36
  • I think what @AnthonyPegram is saying is "No", to your question Isn't "elItem" of type "GridViewItem" and "GVElementos.SelectedItems" a collection of elements of type "GridViewItem"? :-) Check the MSDN doco as suggested, to confirm your understanding of the tools you're using is correct, before asking for help.
    – Sepster
    Feb 24, 2013 at 4:40
  • You are right, I am getting confused by what I know from ASP.NET. I have to read the docs first. Somehow I thought there was going to be friendlier people here. Thanks for taking the time to answer though.
    – Blo Via
    Feb 25, 2013 at 16:50

1 Answer 1

1

That exception is really explicit. You should pay attention to the message instead of pasting code from a previous working implementation. Specially if your implementation is from another framework or environment!

Basically as the guys suggested on the comments of your question and as the exception states you are doing an invalid cast from string to GridViewItem.

Try doing the following:

foreach (string elItem in GVElementos.SelectedItems)
{
     MiColeccion.RemoveAt(GVElementos.Items.IndexOf(elItem));
}

If you read the documentation of the controls you'll understand that each item is a string object.

Here's a quickstart link: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh780650.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

And here's a more concrete example: http://code.msdn.microsoft.com/windowsapps/ListViewSimple-d5fc27dd

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.