본문 바로가기

C# WPF

ListView

<Window x:Class="Movie.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="나의 영화리스트 v.0.1" Height="450" Width="725">

    <Grid>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="360"/>

            <ColumnDefinition/>

        </Grid.ColumnDefinitions>

        <StackPanel Margin="5" Name="Left" >

            <ListView Name="MovieList" Height="210" MouseLeftButtonUp="MovieList_MouseLeftButtonUp" >

                <ListView.View>

                    <GridView>

                        <GridViewColumn  Header="Id" Width="20" DisplayMemberBinding="{Binding ID}" />

                        <GridViewColumn  Header="영화제목" Width="100" DisplayMemberBinding="{Binding 영화제목}" />

                        <GridViewColumn  Header="감독" Width="100" DisplayMemberBinding="{Binding 감독}" />

                        <GridViewColumn  Header="관람일" Width="80" DisplayMemberBinding="{Binding 관람일}" />

                    </GridView>

                </ListView.View>

            </ListView>

            <StackPanel Orientation="Horizontal">

                <TextBox Width="295" Margin="0,5" Name="txtMovie"/>

                <Button Width="48" Margin="5,5" Name="btnMovie" Click="btnMovie_Click">Search</Button>                

            </StackPanel>

        </StackPanel>

        <Grid Margin="5" Grid.Column="1" ShowGridLines="False">

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="115"/>

                <ColumnDefinition/>

                <ColumnDefinition/>

                <ColumnDefinition/>

            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>

                <RowDefinition Height="30"/>

                <RowDefinition Height="30"/>

                <RowDefinition Height="30"/>

                <RowDefinition Height="30"/>

                <RowDefinition Height="30"/>

                <RowDefinition Height="30"/>

                <RowDefinition Height="30"/>

                <RowDefinition Height="30"/>

                <RowDefinition Height="30"/>

                <RowDefinition Height="30"/>

                <RowDefinition Height="30"/>

                <RowDefinition />

            </Grid.RowDefinitions>


            <TextBlock Margin="0,0,5,0" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center">영화제목: </TextBlock>

            <TextBox Margin="2" Grid.ColumnSpan="2" Name="txtTitle" Grid.Column="2"></TextBox>

            <TextBlock Margin="0,0,5,0" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center">국가:</TextBlock>

            <TextBox Margin="2" Grid.Row="1" Grid.ColumnSpan="2" Name="txtCountry"  Grid.Column="2"></TextBox>

            <TextBlock Margin="0,0,5,0" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center">감독:</TextBlock>

            <TextBox Margin="2" Grid.Row="2" Grid.ColumnSpan="2" Name="txt감독"  Grid.Column="2"></TextBox>

            <TextBlock Margin="0,0,5,0" Grid.Row="3" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center">배우:</TextBlock>

            <TextBox Margin="2" Grid.Row="3" Grid.ColumnSpan="2" Name="txt배우"  Grid.Column="2"></TextBox>

            <TextBlock Margin="0,0,5,0" Grid.Row="4" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center">장르:</TextBlock>

            <TextBox Margin="2" Grid.Row="4" Grid.ColumnSpan="2" Name="txt장르"  Grid.Column="2"></TextBox>

            <TextBlock Margin="0,0,5,0" Grid.Row="5" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center">상영시간:</TextBlock>

            <TextBox Margin="2" Grid.Row="5" Grid.ColumnSpan="2" Name="txt상영시간"  Grid.Column="2"></TextBox>

            <TextBlock Margin="0,0,5,0" Grid.Row="6" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center">개봉일:</TextBlock>

            <TextBox Margin="2" Grid.Row="6" Grid.ColumnSpan="2" Name="txt개봉일"  Grid.Column="2"></TextBox>

            <TextBlock Margin="0,0,5,0" Grid.Row="7" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center">관람일:</TextBlock>

            <TextBox Margin="2" Grid.Row="7" Grid.ColumnSpan="2" Name="txt관람일"  Grid.Column="2"></TextBox>

            <TextBlock Margin="0,0,5,0" Grid.Row="8" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center">관람장소:</TextBlock>

            <TextBox Margin="2" Grid.Row="8" Grid.ColumnSpan="2" Name="txt관람장소"  Grid.Column="2"></TextBox>

            <TextBlock Margin="0,0,5,0" Grid.Row="9" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center">동반자:</TextBlock>

            <TextBox Margin="2" Grid.Row="9" Grid.ColumnSpan="2" Name="txt동반자"  Grid.Column="2"></TextBox>

            <TextBlock Margin="0,0,5,0" Grid.Row="10" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center">평점:</TextBlock>

            <TextBox Margin="2" Grid.Row="10" Grid.ColumnSpan="2" Name="txt별점"  Grid.Column="2"></TextBox>

            <TextBlock Margin="5,5,5,0" Grid.Row="10" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Bottom">줄거리/소감문:</TextBlock>

            <TextBox Margin="2" Grid.Row="11" Grid.Column="0" Grid.ColumnSpan="4" Name="txt줄거리" Height="60" ></TextBox>


            <Button Name="btnPoster" Grid.Row="6" Margin="2" Height="26" VerticalAlignment="Bottom" Click="btnPoster_Click">포스터 업로드</Button>

            

            <Button Name="btnInsert" Grid.Row="7" Margin="2" Click="btnInsert_Click">등록</Button>

            <Button Name="btnUpdate" Grid.Row="8" Margin="2" Click="btnUpdate_Click">수정</Button>

            <Button Name="btnDelete" Grid.Row="9" Margin="2" Click="btnDelete_Click">삭제</Button>


            <Image Name="poster" Grid.RowSpan="6"></Image>

            <Border Grid.RowSpan="6" BorderBrush="SteelBlue" BorderThickness="1"></Border>


        </Grid>

    </Grid>

</Window>



using System;

using System.Collections.Generic;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

using System.Data.OleDb;


namespace Movie

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        OleDbConnection conn = null;

        OleDbCommand comm = null;

        OleDbDataReader reader = null;

        string connStr = null;

        private string poster_name = "";


        public class MList

        {

            public int ID { get; set; }

            public string 영화제목 { get; set; }

            public string 감독 { get; set; }

            public string 관람일 { get; set; }

        }


        public MainWindow()

        {

            InitializeComponent();

            connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\bikang\Desktop\Movie\Movie\Movie\MovieDB.accdb;";

            DisplayMovieList();

            LoadPoster();     

        }


        private void LoadPoster()

        {

            var uriSource = new Uri("images/images.jpg", UriKind.Relative);

            poster.Source = new BitmapImage(uriSource);

            poster.Stretch = Stretch.Fill;

        }


        private void DisplayMovieList()

        {            

            if(conn == null)

            {

                conn = new OleDbConnection(connStr);

                conn.Open();

            }


            MovieList.Items.Clear();


            string sql = "SELECT * FROM Movies ORDER BY ID";

            comm = new OleDbCommand(sql, conn);


            reader = comm.ExecuteReader();

            while(reader.Read())

            {             

                List<MList> items = new List<MList>();

                items.Add(new MList() { ID = (int)reader["ID"], 

                    영화제목 = reader["영화제목"].ToString(), 

                    감독 = reader["감독"].ToString(), 

                    관람일 = reader["관람일"].ToString() });

                MovieList.Items.Add(items);

            }

            reader.Close();

            conn.Close();

            conn = null;

        }


        private void MovieList_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

        {

            ListView lv = sender as ListView;

            if (lv.SelectedItem == null) return;


            List<MList> m = lv.SelectedItem as List<MList>;

            int index = m[0].ID;

            MessageBox.Show(m[0].ID.ToString());


            if (conn == null)

            {

                conn = new OleDbConnection(connStr);

                conn.Open();

            }


            string sql = "SELECT * FROM Movies WHERE ID=" + index;

            comm = new OleDbCommand(sql, conn);

            reader = comm.ExecuteReader();


            while (reader.Read())

            {

                txtTitle.Text = reader["영화제목"].ToString();

                txtCountry.Text = reader["국가"].ToString();

                txt감독.Text = reader["감독"].ToString();

                txt배우.Text = reader["배우"].ToString();

                txt장르.Text = reader["장르"].ToString();

                txt상영시간.Text = reader["상영시간"].ToString();

                txt배우.Text = reader["배우"].ToString();

                txt개봉일.Text = reader["개봉일"].ToString();

                txt관람일.Text = reader["관람일"].ToString();

                txt관람장소.Text = reader["관람장소"].ToString();

                txt동반자.Text = reader["동반자"].ToString();

                txt관람일.Text = reader["관람일"].ToString();

                txt별점.Text = reader["별점"].ToString();

                txt줄거리.Text = reader["줄거리"].ToString();  

                string posterStr = "images/" + reader["포스터"].ToString();

                //MessageBox.Show("images/" + reader["포스터"].ToString());

                var uriSource = new Uri(posterStr, UriKind.Relative);

                poster.Source = new BitmapImage(uriSource);

                poster.Stretch = Stretch.Fill;

            }

            reader.Close();

            conn.Close();

            conn = null;

        }


        private void btnPoster_Click(object sender, RoutedEventArgs e)

        {

            // using OpenFileDialog

            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            dlg.DefaultExt = ".jpg";

            dlg.Filter = "Image files (*.png;*.jpg)|*.png;*.jpg|All files (*.*)|*.*"; ;


            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)

            {

                // Open document 

                //string filename = dlg.FileName;   // FileName : 경로까지 가져옴

                string filename = dlg.SafeFileName; // SafeFileName : 파일이름만 가져옴 -> 이 파일 이름을 DB에 넣어야 함

                //MessageBox.Show(filename);

                poster_name = filename;

                //var uriSource = new Uri("images/" + poster_name, UriKind.Relative);

                //poster.Source = new BitmapImage(uriSource);


                BitmapImage bi = new BitmapImage();

                bi.BeginInit();

                bi.UriSource = new Uri("../../images/"+poster_name, UriKind.Relative);

                bi.EndInit();


                poster.Stretch = Stretch.Fill;

                poster.Source = bi;

            }

        }


        private void btnInsert_Click(object sender, RoutedEventArgs e)

        {

            if (txtTitle.Text == "") return;


            if (conn == null)

            {

                conn = new OleDbConnection(connStr);

                conn.Open();

            }


            if (poster_name == "")

                btnPoster_Click(btnPoster, e);


            MessageBox.Show("in Insert " + poster_name);


            string sql = "INSERT INTO Movies(영화제목, 국가, 감독, 배우, 장르, 상영시간, 개봉일, 관람일, 관람장소, 동반자, 별점, 줄거리, 포스터) ";

            sql += "VALUES('" + txtTitle.Text + "', '" + txtCountry.Text + "', '" + txt감독.Text + "', '" + txt배우.Text;

            sql += "', '" + txt장르.Text + "', " + txt상영시간.Text + ", '" + txt개봉일.Text + "', '" + txt관람일.Text;

            sql += "', '" + txt관람장소.Text + "', '" + txt동반자.Text + "', " + txt별점.Text + ", '" + txt줄거리.Text + "', '" + poster_name + "')";

            

            MessageBox.Show(sql);


            comm = new OleDbCommand(sql, conn);

            int x = comm.ExecuteNonQuery();


            conn.Close();

            conn = null;

            DisplayMovieList();

            poster_name = "";

        }


        private void btnDelete_Click(object sender, RoutedEventArgs e)

        {

            if (txtTitle.Text == "") return;


            if (conn == null)

            {

                conn = new OleDbConnection(connStr);

                conn.Open();

            }

            string sql = "DELETE FROM Movies WHERE 영화제목='" + txtTitle.Text + "'";

            comm = new OleDbCommand(sql, conn);


            int x = comm.ExecuteNonQuery();

            if (x == 1)

                MessageBox.Show("정상적으로 삭제되었습니다.");

            conn.Close();

            conn = null;

            DisplayMovieList();

        }


        private void btnUpdate_Click(object sender, RoutedEventArgs e)

        {

            if (conn == null)

            {

                conn = new OleDbConnection(connStr);

                conn.Open();

            }

            string sql = string.Format("UPDATE Movies SET 영화제목='{0}', 국가='{1}', 감독='{2}', 배우='{3}', 장르='{4}', "

                + "상영시간={5}, 개봉일='{6}', 관람일='{7}', 관람장소='{8}', 동반자='{9}', 별점={10}, 줄거리='{11}' WHERE 영화제목='{0}'",

                txtTitle.Text, txtCountry.Text, txt감독.Text, txt배우.Text, txt장르.Text, txt상영시간.Text,

                txt개봉일.Text, txt관람일.Text, txt관람장소.Text, txt동반자.Text, txt별점.Text, txt줄거리.Text);


            MessageBox.Show(sql);

            

            comm = new OleDbCommand(sql, conn);


            int x = comm.ExecuteNonQuery();

            if (x == 1)

                MessageBox.Show("정상적으로 수정되었습니다.");

            conn.Close();

            conn = null;

            DisplayMovieList();

        }


        private void btnMovie_Click(object sender, RoutedEventArgs e)

        {

            Window1 w = new Window1();

            w.Show();

        }

    }

}



'C# WPF' 카테고리의 다른 글

C# Deployment  (0) 2016.09.18
ITQ 그림 파일  (0) 2016.09.12
C#에서의 투명한 색깔의 표현  (0) 2015.12.01
String 클래스와 StringBuilder 클래스의 차이점  (0) 2015.12.01
Stopwatch 사용법  (0) 2015.11.18