Search code examples
c#mysqlwpfbindingconnection-string

How to pass value from DB to Listview/Grid View via binding?


I need to pass a value from a database to Listview via a binding. I managed to make static data by simply adding values to the List and linking them to the desired columns. But I don't understand how to make it so that I could display values from my database (I work through the connection string and MySQL).

What the markup looks like

  <ListView
                    Name="LVBuild">
                    <ListView.View>
                    <GridView>
                        <GridViewColumn 
                            DisplayMemberBinding="{Binding  ID}"
                            Header="ID" />
                        <GridViewColumn
                            DisplayMemberBinding="{Binding Title}"
                            Header="Title" />
                        <GridViewColumn
                            DisplayMemberBinding="{Binding Description}"
                            Header="Description" />
                        <GridViewColumn
                            DisplayMemberBinding="{Binding BuildData}"
                            Header="BuildDate">
                            <GridViewColumn.CellTemplate>

My model

 public class BuildModel
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string BuildData { get; set; }
        public int Architect { get; set; }
        public int Location { get; set; }
        public int Teg { get; set; }
    }

    public class BuildManager
    {
        public static List<BuildModel> GetBuilds()
        {
            List<BuildModel> items = new List<BuildModel>();
            items.Add(new BuildModel() {ID = 1, Title = "Test1", Description = "Desc1", BuildData = "12.12.2022", Architect = 1, Location = 1, Teg = 1});
            items.Add(new BuildModel() {ID = 2, Title = "Test2", Description = "Desc2"});
            items.Add(new BuildModel() {ID = 3, Title = "Test3"});
            items.Add(new BuildModel() {ID = 4, Title = "Test4"});
            return items;
        }
        
    }

How do I pass values from a model

public partial class BuildPageAdmin : Page
    {

        private List<BuildModel> Builds;
        public BuildPageAdmin()
        {
            InitializeComponent();
            LVBuild.ItemsSource = BuildManager.GetBuilds();
        }
    }

Solution

  • Simply use data binding: create a public property to hold the source collection and update it with data from your database. Data binding overview (WPF .NET)

    If your collection is a ObservableCollection you can update it dynamically:

    <Page>
      <ListView ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Page}, Path=Builds}">
    
      </ListView>
    </Page>
    
    public partial class BuildPageAdmin : Page
    {
      // Create a binding source for the ListView
      public ObservableCollection<BuildModel> Builds { get; }
    
      public BuildPageAdmin()
      {
        InitializeComponent();
     
        this.Builds = new ObservableCollection<BuildModel>();
        this.Loaded += OnPageLoaded;
      }
    
      private void OnPageLoaded(object sender, RoutedEventArgs e) 
        => UpdateBuildModels();
    
      // Dynamically update the source collection at any time
      private void UpdateBuildModels()
      {
        this.Builds.Clear();
    
        // Get data from database
        IEnumerable<BuldModel> newBuildModels = BuildManager.GetBuilds();
    
        // Update source collection with new data from the database
        foreach (BuildModel buildModel in newBuildModels)
        {
          this.Builds.Add(bulidModel);
        }
      }
    }