첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ DataSetForm.cs

using System;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// DataSet 폼
    /// </summary>
    public partial class DataSetForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 항공편 바인딩 소스
        /// </summary>
        private BindingSource airplaneBindingSource = new BindingSource(); 

        /// <summary>
        /// 탑승객 바인딩 소스
        /// </summary>
        private BindingSource passengerBindingSource = new BindingSource();

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - DataSetForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public DataSetForm()
        {
            InitializeComponent();

            #region 이벤트를 설정한다.

            Load                                    += Form_Load;
            this.passengerBindingSource.ListChanged += passengerBindingSource_ListChanged;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            DataSet airplaneDataSet = GetAirplaneDataSet();

            DataTable airplaneDataTable  = airplaneDataSet.Tables["Airplane" ];
            DataTable passengerDataTable = airplaneDataSet.Tables["Passenger"];

            this.airplaneBindingSource.DataSource  = airplaneDataSet;
            this.airplaneBindingSource.DataMember  = airplaneDataTable.TableName;

            this.passengerBindingSource.DataSource = airplaneDataSet;
            this.passengerBindingSource.DataMember = passengerDataTable.TableName;

            DataRow airplaneDataRow1;
            DataRow airplaneDataRow2;
            DataRow airplaneDataRow3;

            airplaneDataRow1 = airplaneDataTable.Rows.Add(null, "Boeing 747" , 800 );
            airplaneDataRow2 = airplaneDataTable.Rows.Add(null, "Airbus A380", 1023);
            airplaneDataRow3 = airplaneDataTable.Rows.Add(null, "Cessna 162" , 67  );

            passengerDataTable.Rows.Add(null, airplaneDataRow1["ID"], "Joe Shmuck"    );
            passengerDataTable.Rows.Add(null, airplaneDataRow1["ID"], "Jack B. Nimble");
            passengerDataTable.Rows.Add(null, airplaneDataRow1["ID"], "Jib Jab"       );
            passengerDataTable.Rows.Add(null, airplaneDataRow2["ID"], "Jackie Tyler"  );
            passengerDataTable.Rows.Add(null, airplaneDataRow2["ID"], "Jane Doe"      );
            passengerDataTable.Rows.Add(null, airplaneDataRow3["ID"], "John Smith"    );

            this.airplaneDataGridView.AutoGenerateColumns = true;
            this.airplaneDataGridView.DataSource          = this.airplaneBindingSource;

            this.modelTextBox.DataBindings.Add("Text", this.airplaneBindingSource, "Model");

            this.passengerBindingSource.DataSource = this.airplaneBindingSource;
            this.passengerBindingSource.DataMember = "AirplanePassenger";

            this.passengerListBox.DisplayMember = "Name";
            this.passengerListBox.DataSource    = this.passengerBindingSource;

            this.nameTextBox.DataBindings.Add("Text", this.passengerBindingSource, "Name");
        }

        #endregion
        #region 탑승객 바인딩 소스 리스트 변경시 처리하기 - passengerBindingSource_ListChanged(sender, e)

        /// <summary>
        /// 탑승객 바인딩 소스 리스트 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void passengerBindingSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            if(e.ListChangedType == ListChangedType.Reset)
            {
                this.nameTextBox.Enabled = this.passengerBindingSource.Current != null;
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 항공편 데이터 셋 구하기 - GetAirplaneDataSet()

        /// <summary>
        /// 항공편 데이터 셋 구하기
        /// </summary>
        /// <returns>항공편 데이터 셋</returns>
        private DataSet GetAirplaneDataSet()
        {
            DataSet airplaneDataSet = new DataSet();
            
            DataTable airplaneDataTable = airplaneDataSet.Tables.Add("Airplane");

            airplaneDataTable.Columns.Add("ID"      , typeof(int   ));
            airplaneDataTable.Columns.Add("Model"   , typeof(string));
            airplaneDataTable.Columns.Add("FuelLeft", typeof(int   ));

            DataColumn idDataColumn = airplaneDataTable.Columns["ID"];

            idDataColumn.AutoIncrement     = true;
            idDataColumn.AutoIncrementSeed = 1;
            idDataColumn.AutoIncrementStep = 1;

            DataTable passengerTable = airplaneDataSet.Tables.Add("Passenger");

            passengerTable.Columns.Add("ID"        , typeof(int   ));
            passengerTable.Columns.Add("AirplaneID", typeof(int   ));
            passengerTable.Columns.Add("Name"      , typeof(string));

            idDataColumn = passengerTable.Columns["ID"];

            idDataColumn.AutoIncrement     = true;
            idDataColumn.AutoIncrementSeed = 1;
            idDataColumn.AutoIncrementStep = 1;

            DataRelation dataRelation = airplaneDataSet.Relations.Add
            (
                "AirplanePassenger",
                airplaneDataTable.Columns["ID"],
                passengerTable.Columns["AirplaneID"],
                true
            );

            return airplaneDataSet;
        }

        #endregion
    }
}

 

728x90

 

▶ ObjectCollectionForm.cs

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// Object 컬렉션 폼
    /// </summary>
    public partial class ObjectCollectionForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 항공편 바인딩 소스
        /// </summary>
        private BindingSource airplaneBindingSource = new BindingSource();

        /// <summary>
        /// 탑승객 바인딩 소스
        /// </summary>
        private BindingSource passengerBindingSource = new BindingSource();

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - ObjectCollectionForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public ObjectCollectionForm()
        {
            InitializeComponent();

            #region 이벤트를 설정한다.

            Load                                    += Form_Load;
            this.passengerBindingSource.ListChanged += passengerBindingSource_ListChanged;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 폼 로드시 처리하기 - Form_Load(sender, e)

        /// <summary>
        /// 폼 로드시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void Form_Load(object sender, EventArgs e)
        {
            Airplane airplane1 = new Airplane("Boeing 747" , 800 );
            Airplane airplane2 = new Airplane("Airbus A380", 1023);
            Airplane airplane3 = new Airplane("Cessna 162" , 67  );

            airplaneBindingSource.Add(airplane1);
            airplaneBindingSource.Add(airplane2);
            airplaneBindingSource.Add(airplane3);

            airplane1.PassengerList.Add(new Passenger("Joe Shmuck"    ));
            airplane1.PassengerList.Add(new Passenger("Jack B. Nimble"));
            airplane1.PassengerList.Add(new Passenger("Jib Jab"       ));
            airplane2.PassengerList.Add(new Passenger("Jackie Tyler"  ));
            airplane2.PassengerList.Add(new Passenger("Jane Doe"      ));
            airplane3.PassengerList.Add(new Passenger("John Smith"    ));

            this.airplaneDataGridView.AutoGenerateColumns = true;
            this.airplaneDataGridView.DataSource          = this.airplaneBindingSource;

            this.modelTextBox.DataBindings.Add("Text", airplaneBindingSource, "Model");

            this.passengerBindingSource.DataMember = "PassengerList";
            this.passengerBindingSource.DataSource = this.airplaneBindingSource;

            this.passengerListBox.DisplayMember = "Name";
            this.passengerListBox.DataSource    = passengerBindingSource;

            this.nameTextBox.DataBindings.Add("Text", passengerBindingSource, "Name");

            ((BindingList<Airplane>)this.airplaneBindingSource.List).AllowNew    = true;
            ((BindingList<Airplane>)this.airplaneBindingSource.List).AllowRemove = true;
        }

        #endregion
        #region 탑승객 바인딩 소스 리스트 변경시 처리하기 - passengerBindingSource_ListChanged(sender, e)

        /// <summary>
        /// 탑승객 바인딩 소스 리스트 변경시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void passengerBindingSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            if(e.ListChangedType == ListChangedType.Reset)
            {
                this.nameTextBox.Enabled = this.passengerBindingSource.Current != null;
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요