locked
Grid View RRS feed

  • Question


  • Hi Everyone, I want to create an GridView in windows phone 8.1. Like the grid in "http://www.wpftutorial.net/DataGrid.html" (1st gridview image). I could not get the same gridview

    I my code i tried to obtain the data from Sqlite and binded to the ItemsSource property of gridview in gridview load event of the cs file.   GridView is not supporting AutoGenerateColumns. Is there any other controls for this kind of requirements.

    My Code:

      <Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,9.5,19,0">
                <GridView x:Name="grdUser" HorizontalAlignment="Left"  Height="auto"  VerticalAlignment="Top" Width="auto" Loaded="grdUser_Loaded">
                    <GridView.HeaderTemplate>
                        <DataTemplate>
                            <Border Margin="5" BorderBrush="White"  BorderThickness="1">
                                <Grid >
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="UserName" Grid.Row="0"/>
                                    <TextBlock Text="UserPassword" Grid.Column="1" Grid.Row="0"/>
                                    <TextBlock Text="CreatedDate" Grid.Column="2" Grid.Row="0"/>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </GridView.HeaderTemplate>
                    
                    <GridView.ItemTemplate>
                        <DataTemplate>
                            <Border Margin="5" BorderBrush="White" BorderThickness="1">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock x:Name="tbUserName" Text="{Binding UserName}" Grid.Column="0"/>
                                    <TextBlock x:Name="tbUserPassword" Text="{Binding UserPassword}" Grid.Column="1"/>
                                    <TextBlock x:Name="tbCreatedDate" Text="{Binding CreationDate}" Grid.Column="2"/>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </GridView.ItemTemplate>
                </GridView></Grid>



    Wednesday, October 28, 2015 7:38 AM

All replies

  • Hello, I try to work with your problem.And this is my code.

    Create Customer.cs   file

    It is the code.

        public class Customer
        {
         
    
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Gender { get; set; }
            public string WebSite { get; set; }
            public Boolean ReceiveNewsletter { get; set; }
            public Customer()
            {
    
            }
        }

    And this is the xaml code.

    <Page.Resources>
        <DataTemplate x:Key="MyTable">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock HorizontalAlignment="Left" Text="{Binding FirstName}"></TextBlock>
                    <TextBlock HorizontalAlignment="Left" Grid.Column="1" Text="{Binding LastName}"></TextBlock>
                    <TextBlock HorizontalAlignment="Left" Grid.Column="2" Text="{Binding Gender}"></TextBlock>
                    <TextBlock HorizontalAlignment="Left" Grid.Column="3" Text="{Binding WebSite}"></TextBlock>
            <CheckBox  />
                </Grid>    
        </DataTemplate>
        
    </Page.Resources>
        <Grid>
          <GridView x:Name="MyItemsControl" ItemTemplate="{StaticResource MyTable}"></GridView>
        </Grid>

    Last it is the .cs code.

      public sealed partial class MainPage : Page
        {
            public List<Customer> allcustomers { get; set; }
       
            public MainPage()
            {
                this.InitializeComponent();
    
                this.NavigationCacheMode = NavigationCacheMode.Required;
                allcustomers=new List<Customer>();
                for (int i = 0; i < 10; i++)
                {
                    Customer customers = new Customer();
                    customers.FirstName =string.Format("Jay{0}",i);
                    customers.LastName = string.Format("Ten{0}", i);
                    customers.Gender = string.Format("Male{0}", i);
                    customers.ReceiveNewsletter = false;
                    customers.WebSite = string.Format("www.google.com/{0}", i);
                    allcustomers.Add(customers);
                }
                MyItemsControl.ItemsSource = allcustomers;
            }


    • Edited by Jayden Gu Wednesday, December 30, 2015 8:28 AM
    Wednesday, December 2, 2015 10:00 AM