Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UWP GridView Table

I would like to make like a table in HTML. So I get some data from my database.

Each item is a User. The user has a username, firstname, lastname and email. And I want to make a table to list this users.

Each user have to be on a new line. I have already search on internet, but didn't find anything like I want.

If someone can help me, it will be very nice.

like image 475
Anthony Nfr Avatar asked Jan 21 '16 10:01

Anthony Nfr


People also ask

What is the difference between List View and GridView?

The main difference between ListView and GridView is how it lays out its child. With ListView you are laying your children one by one either vertically or horizontally only. With GridView, its a combination of both. It lays its children horizontally first.

How do I use GridView in XAML?

XAML doesn't support a GridView element. However, you can use a ListView to create a GridView like UI. The code sample in this article is an example of creating GridView in XAML. You can learn more about ListView control here: ListView in WPF.

What is the difference between ListView and GridView in ASP NET?

Programmatic access to the ListView object model to dynamically set properties, handle events, and so on. Multiple key fields. GridView Displays the values of a data source in a table where each column represents a field and each row represents a record.

What is GridView C#?

The GridView control is used to display the values of a data source in a table. Each column represents a field, while each row represents a record. The GridView control supports the following features: Binding to data source controls, such as SqlDataSource.


1 Answers

Use the following skeleton code and build around it (such as adding relevant GridView attributes such as border thickness/color, width, etc. and of course populating data binding from your database). Likewise, attributes for the Grid in the data template.

The code behind for the xaml below is not shown as it contains no user code but page initialization which is added automatically.

This code is tested and works.

<Page x:Class="Sample.GridViewTestPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:data="using:Sample.Data"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d">

    <Page.Resources>
        <data:UserDataCollection x:Key="userDataCollection"/>
    </Page.Resources>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <GridView ItemsSource="{StaticResource userDataCollection}"
                  IsItemClickEnabled="True"
                  IsSwipeEnabled="true"
                  SelectionMode="Single">

            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapGrid Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>

            <GridView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200"/>
                            <ColumnDefinition Width="200"/>
                            <ColumnDefinition Width="200"/>
                            <ColumnDefinition Width="200"/>
                        </Grid.ColumnDefinitions>

                        <TextBlock Grid.Column="0" Text="{Binding UserName}"/>
                        <TextBlock Grid.Column="1" Text="{Binding FirstName}"/>
                        <TextBlock Grid.Column="2" Text="{Binding LastName}"/>
                        <TextBlock Grid.Column="3" Text="{Binding Email}"/>
                    </Grid>
                </DataTemplate>
            </GridView.ItemTemplate>
        </GridView>

    </Grid>
</Page>

The following is the UserData class model and the UserData object collection for binding. It's easy to figure out.

using System.Collections.ObjectModel;

namespace Sample.Data
{
    class UserDataCollection: ObservableCollection<UserData>
    {
        public UserDataCollection()
        {
            // Sample data loaded here. Replace with data from your database

            this.Add(new UserData() {
                UserName = "user1",
                FirstName = "FN1",
                LastName = "LN1",
                Email = "[email protected]" });

            this.Add(new UserData() {
                UserName = "user2",
                FirstName = "FN2",
                LastName = "LN2",
                Email = "[email protected]" });

            this.Add(new UserData() {
                UserName = "user3",
                FirstName = "FN3",
                LastName = "LN3",
                Email = "[email protected]" });
        }
    }

    public class UserData
    {
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
    }
}
like image 159
user5525674 Avatar answered Jan 02 '23 11:01

user5525674