Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

C#: Using ADO.NET Entity Data Model in a WinForms

Hi
Building on an earlier question

I'm trying to create a Winforms Application

Although related I've split this into 2 questions

I can run a query and add a list of All movie titles into a listview but when I click an item I cannot access movie properties to populate the form elements adding a watch to movie
User generated image
User generated image
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MoviesDataModel;

namespace FilmsDB
{
    public partial class Form1 : Form
    {
        public string ImgPath = @"E:\Media\thumbs\Movies\";
        public Form1()
        {
            InitializeComponent();
            TitlesLV.View = View.Details;
            ColumnHeader columnHeader = new ColumnHeader();
            columnHeader.Text = "Film Title";
            columnHeader.TextAlign = HorizontalAlignment.Center;
            columnHeader.Width = -2;

            TitlesLV.Columns.Add(columnHeader);
            LoadMovies();
        }

        private void LoadMovies()
        {
            TitlesLV.Items.Clear();
            using (var context = new MoviesEntities())
            {
                //get a full list of all movies (will return everything from the database)
                var allMovies = context.movies.OrderBy(m => m.title).ToArray();
                foreach(var Movie in allMovies)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = Movie.title;
                    item.Tag = Movie;
                    TitlesLV.Items.Add(item);
                }

            }
        }

        private void TableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void TitlesLV_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = TitlesLV.SelectedIndices[0];
            var  movie = TitlesLV.Items[i].Tag;
            int id = movie.id;
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trevor1940
trevor1940

ASKER

Thanx saige

Can I ask is  this the correct way of doing this  n terms of best practice?

The list of people in each movie is in a separate table with a joining table

How do I access these? They don't seem to be accessible in the Movie object would this be a separate query?
With the way you have your solution designed this is a perfectly viable way of storing your objects.  As for the joined data, yes you would want to use a separate query.

-saige-