I cannot implement a Sfdatagrid

Asked

Viewed 21 times

0

Trying to follow a tutorial here to create a Sfdatagrid, but it’s not working. I’m very doubtful about how to implement it. My Model Class

namespace Autorizador.Model
    {
        public class LiberacaoGrid
        {
            private double dataLib;
            private string cliente;
            private string vendedor;
            private string filial;

            public double DataLib
            {
                get { return dataLib; }
                set { this.dataLib = value; }
            }
            public string Cliente
            {
                get { return cliente; }
                set { this.cliente = value; }
            }
            public string Vendedor
            {
                get { return vendedor; }
                set { this.vendedor = value; }
            }
            public string Filial
            {
                get { return filial; }
                set { this.filial = value; }
            }
            public LiberacaoGrid(double datalib, string cliente, string vendedor, string filial)
            {
                this.DataLib = datalib;
                this.Cliente = cliente;
                this.Vendedor = vendedor;
                this.Filial = filial;
            }
        }

Another class called a repository

namespace Autorizador.Model
{
    public class LiberacaoRepository
    {
        DataService dataService;
        List<Liberacao> lib;
        private ObservableCollection<LiberacaoGrid> orderInfo;
        public ObservableCollection<LiberacaoGrid> OrderInfoCollection
        {
            get { return orderInfo; }
            set { this.orderInfo = value; }
        }
        public LiberacaoRepository()
        {
            dataService = new DataService();
            orderInfo = new ObservableCollection<LiberacaoGrid>();
            this.AtualizaDados();
        }
        public async void AtualizaDados()
        {
            lib = await dataService.GetLiberaAsync();
        }

    }

My App.xaml.Cs

namespace Autorizador
{
    public partial class App : Application
    {
        SfDataGrid dataGrid;
        public App()
        {
            InitializeComponent();

            dataGrid = new SfDataGrid();
            MainPage = new ContentPage { Content = dataGrid };
        //MainPage = new Autorizador.MainPage();
        //MainPage = new NavigationPage(new Autorizador.MainPage());
    }


    protected override void OnStart()
    {
        //CrossPushNotification.Current.Unregister();
        //CrossPushNotification.Current.Register();
    }

    protected override void OnSleep()
    {
        // Handle when your app sleeps
    }

    protected override void OnResume()
    {
        // Handle when your app resumes
    }
}

Mainpage

DataService dataService;
        List<Liberacao> lib;
        double _idorcamento = 0d;
        LiberacaoRepository lRepository = new LiberacaoRepository();

        public MainPage()
    {
        InitializeComponent();
        try
        {
            dataService = new DataService();

            AtualizaDados();
            //Content = new ScrollView { Content = listaLibera, Orientation = ScrollOrientation.Both };
        }
        catch(Exception ex)
        {
            string erro = ex.Message;
        }
    }

    private async void AtualizaDados()
    {
        try
        {
            lib = await dataService.GetLiberaAsync();
            //listaLibera.ItemsSource = lib.OrderBy(item => item.Cliente).ToList();
            dataGrid.ItemsSource = lib.OrderBy(item => item.Cliente).ToList(); //lRepository.OrderInfoCollection;
        }
        catch (Exception ex)
        {
            string erro = ex.Message;
        }
    }

I know there’s something missing from that code, but I don’t know what it is

1 answer

0

I got it this way:

SfDataGrid dataGrid;
        DataService dataService = new DataService();
        public App()
        {
            InitializeComponent();

            dataGrid = new SfDataGrid();
            MainPage = new ContentPage { Content = dataGrid };
            this.CriaDataGrid();

            //MainPage = new Autorizador.MainPage();
            //MainPage = new NavigationPage(new Autorizador.MainPage());
        }

        public async void CriaDataGrid()
        {
            dataGrid.AutoGenerateColumns = false;

            GridTextColumn dataLib = new GridTextColumn();
            dataLib.MappingName = "DataLib";
            dataLib.HeaderText = "Data";
            dataLib.Width = 80;
            dataLib.TextAlignment = TextAlignment.Center;

            GridTextColumn cliente = new GridTextColumn();
            cliente.MappingName = "Cliente";
            cliente.HeaderText = "Cliente";
            cliente.Width = 180;
            cliente.TextAlignment = TextAlignment.Center;

            GridTextColumn vendedor = new GridTextColumn();
            vendedor.MappingName = "Vendedor";
            vendedor.HeaderText = "Vendedor";
            vendedor.Width = 180;
            vendedor.TextAlignment = TextAlignment.Center;

            GridTextColumn filial = new GridTextColumn();
            filial.MappingName = "Filial";
            filial.HeaderText = "Filial";
            filial.Width = 100;
            filial.TextAlignment = TextAlignment.Center;

            dataGrid.Columns.Add(dataLib);
            dataGrid.Columns.Add(cliente);
            dataGrid.Columns.Add(vendedor);
            dataGrid.Columns.Add(filial);

            //dataGrid.AllowResizingColumn = true;
            dataGrid.ItemsSource = await dataService.GetLiberaAsync();
        }

Browser other questions tagged

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