Sunday, April 3, 2011

WPF Listview bound with Hashtable

I already bind a listview (grid) with hashtable, so how can I get items from there? Before using hashtable I just did ListViewA.SelectedItems; and I was getting the source.

Ir fails when I use two foreach loops:

dlstTemplates is my ListView in WPF

foreach (var group in dlstTemplates.SelectedItems)
{
  foreach (var Template in group)
  {

  }
}

Error 2 foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator' D:\cs_InformeMedico\app\Template.xaml.cs 85 21 Demo.View

I found this on debugger:

-    dlstPlantillas.SelectedItems Count = 1 System.Collections.IList {System.Windows.Controls.SelectedItemCollection}
-    [0] {System.Data.Linq.SqlClient.ObjectReaderCompiler.Group<string,Demo.View.Plantilla>} object {System.Data.Linq.SqlClient.ObjectReaderCompiler.Group<string,Demo.View.Plantilla>}
-    Non-Public members  
+    items Count = 97 System.Collections.Generic.IEnumerable<Demo.View.Plantilla> {System.Collections.Generic.List<Demo.View.Plantilla>}
     key "101010112000" string
     System.Linq.IGrouping<K,T>.Key "101010112000" string
-    Results View Expanding the Results View will enumerate the IEnumerable 
+    [0] {Demo.View.Plantilla} Demo.View.Plantilla
+    [1] {Demo.View.Plantilla} Demo.View.Plantilla
+    [2] {Demo.View.Plantilla} Demo.View.Plantilla
+    [3] {Demo.View.Plantilla} Demo.View.Plantilla
+    [4] {Demo.View.Plantilla} Demo.View.Plantilla
+    [5] {Demo.View.Plantilla} Demo.View.Plantilla
+    [6] {Demo.View.Plantilla} Demo.View.Plantilla
+    [7] {Demo.View.Plantilla} Demo.View.Plantilla
+    [8] {Demo.View.Plantilla} Demo.View.Plantilla
+    [9] {Demo.View.Plantilla} Demo.View.Plantilla
+    [10] {Demo.View.Plantilla} Demo.View.Plantilla
-    Raw View  
-    [System.Windows.Controls.SelectedItemCollection] Count = 1 System.Windows.Controls.SelectedItemCollection
+    [0] {System.Data.Linq.SqlClient.ObjectReaderCompiler.Group<string,Demo.View.Plantilla>} object {System.Data.Linq.SqlClient.ObjectReaderCompiler.Group<string,Demo.View.Plantilla>}
+    Raw View  
     IsFixedSize false bool
     IsReadOnly false bool

So when group is a

{System.Data.Linq.SqlClient.ObjectReaderCompiler.Group<string,Demo.View.Plantilla>} object type

Sorry Plantilla = Template, I traduce it for more understand

From stackoverflow
  • You don't seem to be working with a collection in the second loop.
    Maybe you wanted

    foreach(var group in dlstTemplates.SelectedItems)
    {
        foreach(var Template in groupCast.Templates)
        {
            //do stuff...
        }
    }
    

    or somesuch.

    Angel Escobedo : I cant get Templates as property in groupCast, so I Have to add a Default property in my Template Class named Templates?
    Angel Escobedo : when I try that I get Error 1 'Template': member names cannot be the same as their enclosing type D:\cs_InformeMedico\app\Template.cs 26 26 Demo.View
    ZombieSheep : No. You were trying to enumerate a single object in the 2nd loop. You use foreach you must work on a collection. foreach(var thing in (int)17) will yield the same error you were getting.
    ZombieSheep : Perhaps if you posted the code for your collection member in dlstTemplates.SelectedItems) we could sort this out
  •             foreach (var group in dlstPlantillas.SelectedItems)
                {
                    IGrouping<string, Plantilla> groupCast = group as System.Linq.IGrouping<string, Plantilla>;
                    if (null == groupCast) return;
                    foreach (Plantilla item in groupCast)
                    {
                        template.codigoestudio = item.codigoestudio;
    
                    }
                }
    

    hopes helps... thanks ZombieSheep for inspiration!

0 comments:

Post a Comment