View All Columns from an Sqlite Table using Bindig

Asked

Viewed 222 times

1

Forms, I would like to know how to display all table columns of a Sqlite database, using Binding and Listview. I’m using Visual Studio 2017, the latest version with Android. All the features of Include, Delete and Change and List are working. I just can’t display all table columns. My registration page, Cadastropage.xaml, has the following code:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="AppResumoAulas.Pages.CadastroPage"
             Title="Cadastro de Usuários">
    <ContentPage.ToolbarItems>
        <ToolbarItem Name="Novo" Icon="new48.png" Activated="Novo_Activated" />
    </ContentPage.ToolbarItems>
    <ContentPage.Content>
        <StackLayout>
            <SearchBar Placeholder="Procurar..."  BackgroundColor="Yellow"
               TextChanged="SearchBar_TextChanged" />
            <ListView x:Name="lvwUsuarios">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextCell Text="{Binding Usu_Id}" Detail="{Binding Usu_Nome, Usu_Email}" 
                          TextColor="Black" 
                          DetailColor="Silver">
                            <TextCell.ContextActions>
                                <MenuItem Text="Alterar" Clicked="Alterar_Clicked" 
                                  CommandParameter="{Binding .}" />
                                <MenuItem Text="Deletar" Clicked="Deletar_Clicked" 
                                  IsDestructive="True" 
                                  CommandParameter="{Binding .}" />
                            </TextCell.ContextActions>
                        </TextCell>                        
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

The code of the page Cadastropage.xaml.Cs, follows below:

using AppResumoAulas.Model;
using System;
using System.Collections.ObjectModel;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace AppResumoAulas.Pages
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CadastroPage : ContentPage
    {
        private ObservableCollection<Usuarios> _usuarios;

        public CadastroPage ()
        {
            InitializeComponent ();
            var usuarios = App.DataBase.GetUsuarios();
            _usuarios = new ObservableCollection<Usuarios>(usuarios);
            lvwUsuarios.ItemsSource = _usuarios;
        }

        private void Novo_Activated(object sender, EventArgs e)
        {
            Navigation.PushAsync(new IncluirPage(_usuarios));
        }

        private void Alterar_Clicked(object sender, EventArgs e)
        {
            var menuItem = sender as MenuItem;
            var usuario = menuItem.CommandParameter as Usuarios;
            Navigation.PushAsync(new AlterarPage(usuario));
        }

        private async void Deletar_Clicked(object sender, EventArgs e)
        {
            var usuario = (sender as MenuItem).CommandParameter as Usuarios;
            var resposta = await DisplayAlert("Confirma Exclusão deste registro ?", usuario.Usu_Nome, "Sim", "Não");
            if (resposta == true)
            {
                App.DataBase.DeletarUsuario(usuario);
                _usuarios.Remove(usuario);
            }
        }

        private void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
        {
            lvwUsuarios.ItemsSource = App.DataBase.GetUsuarios(e.NewTextValue);
        }
    }
}

The code of the creation of the Table, follows below:

using SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace AppResumoAulas.Model
{
    [Table("Usuarios")]
    public class Usuarios : NotifyBase
    {
        [PrimaryKey, AutoIncrement]
        public int Usu_Id { get; set; }

        private string _Usu_Nome;
        [MaxLength(300)]
        public string Usu_Nome
        {
            get { return _Usu_Nome; }
            set
            {
                _Usu_Nome = value;
                OnPropertyChanged();
            }
        }

        private string _Usu_Email;
        [MaxLength(150)]
        public string Usu_Email
        {
            get { return _Usu_Email; }
            set
            {
                _Usu_Email = value;
                OnPropertyChanged();
            }
        }

        private bool _Usu_Ativo;
        public bool Usu_Ativo
        {
            get { return _Usu_Ativo; }
            set
            {
                _Usu_Ativo = value;
                OnPropertyChanged();
            }
        }
    }
}

It seems a simple problem, for me, that I am starting, and very complex. Waiting for who can help. Very grateful to all.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.