Display Tabular Data using Listview o Gridview RRS feed

  • Question

  • I have tabular data that I want to display using list view o grid view. can any one guide me.
    Friday, December 21, 2012 9:55 AM


  • You can display tabular data by using GridView.ItemTemplate, I've created a simple example for displaying tabular data using the grid view:

    This is a sample class

     public class SampleData
            private int id;
            private string name;
            private string address;
            public int Id
                get { return this.id; }
                set { this.id = value; }
            public string Name
                get { return this.name; }
                set { this.name = value; }
            public string Address
                get { return this.address; }
                set { this.address = value; }
            public SampleData(int id, string name, string address)
                this.id = id;
                this.name = name;
                this.address = address;
            public static ObservableCollection<SampleData> CreateSampleData()
                var smapleDataCollection = new ObservableCollection<SampleData>();
                smapleDataCollection.Add(new SampleData(1, "Micheal", "United States"));
                smapleDataCollection.Add(new SampleData(2, "Emma", "England"));
                smapleDataCollection.Add(new SampleData(3, "Mohammad", "Jordan"));
                smapleDataCollection.Add(new SampleData(4, "Jack", "France"));
                return smapleDataCollection;

    And in xaml page:

            <CollectionViewSource x:Name="sampleDataSource" Source="{Binding SampleData}" />
     <GridView Grid.Row="1" ItemsSource="{Binding Source={StaticResource sampleDataSource}}">
                                <ColumnDefinition Width="100"/>
                                <ColumnDefinition Width="100"/>
                                <ColumnDefinition Width="100"/>
                            <TextBlock Text="{Binding Id}"  />
                            <TextBlock Grid.Column="1" Text="{Binding Name}" />
                            <TextBlock Grid.Column="2" Text="{Binding Address}" />

    then in xaml.cs:

    this.DefaultViewModel["SampleData"] = SampleData.CreateSampleData();
    Hope that helps.

    • Proposed as answer by Jesse Jiang Thursday, December 27, 2012 8:03 AM
    • Marked as answer by JAsh Parekh Friday, December 28, 2012 9:47 AM
    Monday, December 24, 2012 6:58 AM