* "윈폼"에서 '새 프로젝트 만들기'로 프로젝트 실행



==> Form1.cs [디자인]







-------------------------------------------------------------------------------------




==> Form1.cs




using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace MakeTable

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private DataTable dt; //

        private void tblPeople_Click(object sender, EventArgs e)

        {

            dt = MakePeopleTable(); //

            dataGridView1.DataSource = dt; //

        }

 

        private DataTable MakePeopleTable()

        {

            DataTable tblPeople = new DataTable("tblPeople");

 

            DataColumn col;

            DataRow row;

 

            // 열 등록

            col = new DataColumn("Name", typeof(String));

            col.MaxLength = 10;

            col.AllowDBNull = false;

            col.Unique = true;

            tblPeople.Columns.Add(col);

 

            tblPeople.PrimaryKey = new DataColumn[] { col };

 

            col = new DataColumn("Age", typeof(Int32));

            col.AllowDBNull = false;

            tblPeople.Columns.Add(col);

 

            col = new DataColumn("Male", typeof(bool));

            col.AllowDBNull = false;

            tblPeople.Columns.Add(col);

 

            // 행 삽입

            row = tblPeople.NewRow();

            row["Name"] = "정우성";

            row["Age"] = 36;

            row["Male"] = true;

            tblPeople.Rows.Add(row);

 

            row = tblPeople.NewRow();

            row["Name"] = "고소영";

            row["Age"] = 32;

            row["Male"] = false;

            tblPeople.Rows.Add(row);

 

            row = tblPeople.NewRow();

            row["Name"] = "배용준";

            row["Age"] = 37;

            row["Male"] = true;

            tblPeople.Rows.Add(row);

 

            row = tblPeople.NewRow();

            row["Name"] = "김태희";

            row["Age"] = 29;

            row["Male"] = false;

            tblPeople.Rows.Add(row);

 

            tblPeople.AcceptChanges();

            return tblPeople;

        }

 

        private void tblSale_Click(object sender, EventArgs e)

        {

            DataTable tblSale = MakeSaleTable();

            dataGridView1.DataSource = tblSale;

        }

 

        private DataTable MakeSaleTable()

        {

            DataTable tblSale = new DataTable("tblSale");

 

            DataColumn col;

            DataRow row;

 

            //열 등록

            col = new DataColumn("OrderNo", typeof(Int32));

            col.AllowDBNull = false;

            col.Unique = true;

            col.AutoIncrement = true;

            col.ReadOnly = true;

            tblSale.Columns.Add(col);

 

            tblSale.PrimaryKey = new DataColumn[] { col };

 

            col = new DataColumn("Customer", typeof(String));

            col.MaxLength = 10;

            col.AllowDBNull = false;

            tblSale.Columns.Add(col);

 

            col = new DataColumn("Item", typeof(String));

            col.MaxLength = 20;

            col.AllowDBNull = false;

            tblSale.Columns.Add(col);

 

            col = new DataColumn("ODate", typeof(DateTime));

            col.AllowDBNull = false;

            tblSale.Columns.Add(col);

 

            // 행 삽입

            row = tblSale.NewRow();

            row["Customer"] = "정우성";

            row["Item"] = "면도기";

            row["ODate"] = new DateTime(2008, 1, 1);

            tblSale.Rows.Add(row);

 

            row = tblSale.NewRow();

            row["Customer"] = "고소영";

            row["Item"] = "화장품";

            row["ODate"] = new DateTime(2008, 1, 2);

            tblSale.Rows.Add(row);

 

            row = tblSale.NewRow();

            row["Customer"] = "김태희";

            row["Item"] = "핸드폰";

            row["ODate"] = new DateTime(2008, 1, 3);

            tblSale.Rows.Add(row);

 

            row = tblSale.NewRow();

            row["Customer"] = "김태희";

            row["Item"] = "휘발유";

            row["ODate"] = new DateTime(2008, 1, 4);

            tblSale.Rows.Add(row);

 

            tblSale.AcceptChanges();

            return tblSale;

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            // 1 2열짜리 테이블 만들기 : 홍길동, 백두산

            DataTable dt = new DataTable();

 

            DataRow dr = dt.NewRow(); // 팩터리 메서드???

 

            // 열 하나 만들기

            DataColumn dc = new DataColumn("Name", typeof(String)); // VarChar

            dc.MaxLength = 25; // VarChar(25)

            dc.AllowDBNull = false; // Not Null

 

            // 테이블에 열을 추가

            dt.Columns.Add(dc);

 

            // 행 하나 만들기

            dr["Name"] = "홍길동";

            dt.Rows.Add(dr);

 

            this.dataGridView1.DataSource = dt; // 데이터테이블 바인딩...

 

        }

 

        private void DisplayResult(DataRow[] Result)

        {

            listBox1.Items.Clear();

            foreach (DataRow R in Result)

            {

                listBox1.Items.Add(R["Name"]);

            }

        }

 

        private void btnAsc_Click(object sender, EventArgs e)

        {

            // 오름차순 정렬

            DataRow[] Result = dt.Select("", "Name Asc");

            DisplayResult(Result); // 출력

        }

 

        private void btnDesc_Click(object sender, EventArgs e)

        {

            DataRow[] Result = dt.Select("", "Name Desc"); // "Select메서드" RowFilter + Sort 속성 둘 다 가지고 있다.

            DisplayResult(Result); // 출력

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

 

        }

 

        private void btnFind_Click(object sender, EventArgs e)

        {

            // 이름 가져오기

            string name = textName.Text;

            DataRow Result = dt.Rows.Find(name); // 고유키에서 특정 레코드 찾기

            if (Result == null)

            {

                MessageBox.Show("해당 사람이 없습니다.");

            }

            else

            {

                MessageBox.Show(name + "의 나이 : " + Result["Age"].ToString());

            }

        }

 

        private void btnSelect_Click(object sender, EventArgs e)

        {

            DataRow[] Result = dt.Select("Age >= " + textAge.Text);

            DisplayResult(Result); // 출력

        }

 

        private void btnUpdate_Click(object sender, EventArgs e)

        {

            DataRow[] arr = dt.Select("Name = '" + textName2.Text + "'");

            if (arr.Length != 0)

            {

                arr[0]["Age"] = Convert.ToInt32(textAge2.Text);

            }

            this.dataGridView1.DataSource = dt;

        }

 

        private void btnDelete_Click(object sender, EventArgs e)

        {

            DataRow[] arr = dt.Select("Name = '" + textName3.Text + "'");

            if (arr.Length != 0)

            {

                arr[0].Delete(); // 현재 레코드 삭제

            }

            this.dataGridView1.DataSource = dt;

        }

 

        private void btnAccept_Click(object sender, EventArgs e)

        {

            dt.AcceptChanges(); // 변경완료

        }

 

        private void btnReject_Click(object sender, EventArgs e)

        {

            dt.RejectChanges(); // 변경취소

        }

    }

}

 

 

// "Select메서드" "다중값" 반환  / "Find메서드" "단일값(1개 레코드값)" 반환.

// "Select메서드" DataRow를 반환한다.

// "Select메서드" 2가지 속성(RowFilter속성과 Sort 속성)을 가지고 있다.

 

 

 


-------------------------------------------------------------------------------------




[ 실행결과 ]








Posted by holland14
: