Problem to the popular Datagrid

Asked

Viewed 64 times

1

I’m making the population of a Datagrid, but I don’t know why you’re not showing.

Result:

Resultado

Only empty lines are returned.

Fiscalizacao.Cs

public class Fiscalizacoes
    {
        private String Concessionaria { get; set; }
        private String Municipio { get; set; }
        private String Sistemas { get; set; }
        private DateTime DataInicio { get; set; }
        private DateTime DataFim { get; set; }
        private String Status { get; set; }

        public Fiscalizacoes()
        {

        }

        public Fiscalizacoes(String concessionaria, String municipio, String sistemas, DateTime datainicio,
            DateTime datafim, String status)
        {
            Concessionaria = concessionaria;
            Municipio = municipio;
            Sistemas = sistemas;
            DataInicio = datainicio;
            DataFim = datafim;
            Status = status;
        }
    }

populaGrid()

private static IEnumerable<Fiscalizacoes> popularGrid()
        {
            var listaFiscalizacao = new List<Fiscalizacoes>
            {
                new Fiscalizacoes("SABESP", "São Paulo", "Abastecimento Água", new DateTime(2014, 12, 1),
                    new DateTime(2014, 12, 10), "Não Respondido"),
                new Fiscalizacoes("Mairinque", "Taubaté", "Esgotamento Sanitário", new DateTime(2014, 12, 1),
                    new DateTime(2014, 12, 10), "Respondido"),
                new Fiscalizacoes("Foz Gertrudes", "São José dos Campos", "Esgotamento Sanitário", new DateTime(2014, 12, 1),
                    new DateTime(2014, 12, 10), "Transmitido")
            };

            return listaFiscalizacao;
        }

XAML

<DataGrid x:Name="dataGridFiscalizacoes" ItemsSource="{Binding listaFiscalizacao}" AutoGenerateColumns="False" >
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Concessionária" Binding="{Binding Concessionaria}" />
                    <DataGridTextColumn Header="Município" Binding="{Binding Municipio}" />
                    <DataGridTextColumn Header="Sistemas" Binding="{Binding Sistemas}" />
                    <DataGridTextColumn Header="Data Início" Binding="{Binding DataInicio}" />
                    <DataGridTextColumn Header="Data Fim" Binding="{Binding DataFim}" />
                    <DataGridTextColumn Header="Status Checklist" Binding="{Binding Status}" />
                </DataGrid.Columns>
            </DataGrid>

window

 public ChecklistListagem(InicioWindow w)
        {
            InitializeComponent();
            this.w = w;
            dataGridFiscalizacoes.ItemsSource = popularGrid();
        }
  • At some event you are associating the Itemsource? dataGridFiscalizacoes.ItemsSource = popularGrid();

  • @Diegozanardo Yes, I’m associating. I forgot to put...

  • 1

    Have you tried to change the scope of your properties to public?

  • @Jonathan shudler was just that!

1 answer

1


I don’t usually work with the Binding this way. I will put down the way I use it.

First:

In the DataGrid just point Itemssource to Binding. And add to Binding the Path:

    <DataGrid x:Name="dataGridFiscalizacoes" ItemsSource="{Binding}" AutoGenerateColumns="False" >
<DataGrid.Columns>
                    <DataGridTextColumn Header="Concessionária" Binding="{Binding Path=Concessionaria, Mode=OneWay}" />
                    <DataGridTextColumn Header="Município" Binding="{Binding Path=Municipio, Mode=OneWay}" />
                    <DataGridTextColumn Header="Sistemas" Binding="{Binding Path=Sistemas, Mode=OneWay}" />
                    <DataGridTextColumn Header="Data Início" Binding="{Binding Path=DataInicio, Mode=OneWay}" />
                    <DataGridTextColumn Header="Data Fim" Binding="{Binding Path=DataFim, Mode=OneWay }" />
                    <DataGridTextColumn Header="Status Checklist" Binding="{Binding Path=Status, Mode=OneWay}" />
                </DataGrid.Columns>

According to:

You need at some event associate the ItemsSource. I’m going to assume it’s in the builder’s Window:

public MinhaWindow(){
    dataGridFiscalizacoes.ItemsSource = popularGrid();
}

Third:

As @Jonathan Shudler posted in the comments the class properties need to be public:

public class Fiscalizacoes
    {
        public String Concessionaria { get; set; }
        public String Municipio { get; set; }
        public String Sistemas { get; set; }
        public DateTime DataInicio { get; set; }
        public DateTime DataFim { get; set; }
        public String Status { get; set; }
}
  • The Path It’s not right, it’s wrong: Type 'Path' is used like a markup extension but does not derive from MarkupExtension.

  • I forgot the = after Path. Now try doing please!

  • It was no mistake, but it also showed nothing, remains the same

  • Add the Mode in the Binding. For example Mode=OneWay

  • Still the same, rs

  • I just saw the comment from @Jonathan Shudler, I was testing here, had arrived at it... rsrsrs

Show 1 more comment

Browser other questions tagged

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