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

trevor1940
trevor1940 used Ask the Experts™
on
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
MovieID.JPG
MovieID1.JPG
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Because Tag is an object, you have to cast it to the concrete type you are trying to access; e.g. -
var  movie = TitlesLV.Items[i].Tag as Movie;

Open in new window

-saige-

Author

Commented:
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?

Commented:
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-

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial