none
Assign List<string> of Class to GridView

    Question

  • I am writing to ask if it is possible to assign the attributes of a List of Strings, contained within a C# class, to a GridView;

    In the requirements of our project, we are trying to not pre-define properties in a C# class - rather build the C# class dynamically, based on the results from a Json.Net call to an ASP.Net Web API, which communicates with an SQL Server database; 

    this.DataGrid.ItemsSource = newListInstance - at this line, in debug mode, I can see the values in the public List<string> Attributes; however, I have been unable to successfully navigate to the level where Attibutes reside;

    newListInstance is the instance of the class MyClass, and if bound to the GridView, displays two columns (Metric_Display_Name and Attributes);  The Attibutes column displays 22 rows of System.Collections.Generic.List'1[System.String]

    Thanks in advance for any insight, guidance;

    List<MyClass> newListInstance = new List<MyClass>();

    foreach (var child in jObj.Children())
    
                        {
    
                            MyClass instance = new MyClass();
    
                            for (int j = 0; j < child.Count; j++)
    
                            {
    
                                instance.AddAttribute((string)child[j]);                            
    
                            }
    
                            newListInstance.Add(instance);
    
    }

    class MyClass
        {
            public string Metric_Display_Name
            {
                get;
                set;
            }
     
            public List<string> Attributes
            {
                get;
                private set;
            }
     
            public MyClass()
            {
                Attributes = new List<string>();
            }
     
            public void AddAttribute(string value)
            {
                Attributes.Add(value);
            }
     
     
     
        }

    robert hellestrae

    Monday, August 05, 2013 3:08 PM

Answers

  • Hi,rrobhelle

    You can set a listview Control in GridView DataTemplate.In XAML,you can Code like this:

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <GridView x:Name="gridview" ItemsSource="{Binding}"> <GridView.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Metric_Display_Name}"> </TextBlock> <ListView ItemsSource="{Binding Attributes}"> </ListView> </StackPanel> </DataTemplate> </GridView.ItemTemplate> <GridView.ItemsPanel> <ItemsPanelTemplate> <WrapGrid Orientation="Horizontal"></WrapGrid> </ItemsPanelTemplate> </GridView.ItemsPanel> </GridView> </Grid>

    Then in code-behind  you can  set the gridview DataContext to newListInstance .I do not know what value is contained in your Attributes,so I add some value to Attributes to test the Project,I code like this :

    public MainPage()
            {
                this.InitializeComponent();
                List<MyClass> newListInstance = new List<MyClass>();
                List<string> arrriutes= new List<string>();
                arrriutes.Add("yyyy");
                arrriutes.Add("yyyy");
                arrriutes.Add("yyyy");
                newListInstance.Add(new MyClass(arrriutes, "66"));
                newListInstance.Add(new MyClass(arrriutes, "66"));
                newListInstance.Add(new MyClass(arrriutes, "66"));
                gridview.DataContext = newListInstance;
                
            }
    
         
        }
        class MyClass
        {
            public string Metric_Display_Name
            {
                get;
                set;
            }
    
            public List<string> Attributes
            {
                get;
                private set;
            }
    
            public MyClass(List<string> arrriutes, string metric_Display_Name)
            {
                Attributes = arrriutes;
                Metric_Display_Name = metric_Display_Name;
            }
    
        }
    
    I hope this can help you.
    Best wishes!
    • Marked as answer by rrobhelle Tuesday, August 06, 2013 1:03 PM
    Tuesday, August 06, 2013 5:44 AM