0

I want to bind certain fields from class to DataGrid in WPF through DataSource but now there is problem, fields bind properly but there are also fields from class that I don't want how can I prohibit this behavior?

Method where I bind them to DataGrid.

string[] tabelaPredmetBinding = { "Naziv", "Kratica", "Ects" };    
//method where I try to bind data to grid

    private void predmet_load()
    {
        DataGridPodatki.Columns.Clear();

        for (int i = 0; i < tabelaPredmet.Length; i++) {
            DataGridTextColumn textColumn = new DataGridTextColumn();
            textColumn.Header = tabelaPredmet[i];
            textColumn.Binding = new Binding(tabelaPredmetBinding[i]);
            DataGridPodatki.Columns.Add(textColumn);
        }

        /*foreach (var item in servis.vrniVsePredmete()) {
            DataGridPodatki.Items.Add(item);
        }*/

        DataGridPodatki.ItemsSource = servis.vrniVsePredmete();

    }

Class that get's returned from SOAP service

[DataContract]
public class Predmet
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [DataMember]
    [Key]
    public int Id { get; set; }
    [DataMember]
    public string Naziv { get; set; }
    [DataMember]
    public string Kratica{get; set;}
    [DataMember]
    public int Ects { get; set; }
    [DataMember]
    public virtual ICollection<StudentImaPredmet> studentiImajoPredmet { get; set; }

    public Predmet()
    {
        studentiImajoPredmet = new HashSet<StudentImaPredmet>();
    }

    public Predmet(string naziv, string kratica, int ects)
    {
        Naziv = naziv;
        Kratica = kratica;
        Ects = ects;
    }

}

This is what I get, I only want "Naziv","Kratica","ECTS"

ASh
  • 34,632
  • 9
  • 60
  • 82
renekton
  • 61
  • 1
  • 8

1 Answers1

1

DataGrid has AutoGenerateColumns with default value true - a value that indicates whether the columns are created automatically.

set it to false before assigning ItemsSource:

DataGridPodatki.AutoGenerateColumns = false;
DataGridPodatki.ItemsSource = servis.vrniVsePredmete();

also: please see about the difference of properties and fields, and importance of properties in bindings.

ASh
  • 34,632
  • 9
  • 60
  • 82