본문 바로가기

프로젝트/DataGridView XML 데이타 베이스 프로그램

DataGrid 프로그램 ( XML DataBase 편집 프로그램 )중요 소스 (2)

 

중요 소스코드 분석  ver 1.01

 

using DataGrid;
using System;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms; ///   연구용   ///   


namespace DataGridXML
{
    public partial class Form1 : Form
    {
        bool              NoEdit = true;   // 변경 내용이 없음
        DataSet        ds;                    //  작업 DataSet 
        DataTable     dt;                     //  작업 Data Table 
        string            df;                     //  작업 File Path (파일 이름 포함 경로)
        string            wf = Application.StartupPath;   // 작업 경로


        private void Form1_Load(object sender, EventArgs e)
        {          
            dataGrid.Font = new Font("돋음체", 12, FontStyle.Regular);
            // Grid의 속성설정
            foreach (DataGridViewColumn column in dataGrid.Columns)
            {  
                column.SortMode = DataGridViewColumnSortMode.NotSortable;
                column.HeaderCell.Style.Alignment= DataGridViewContentAlignment.MiddleCenter;
                column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }
            NoEdit = true;
            ControlEnable();  // 콘트롤 Enabled속성 설정     
        }  //func



        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {          
            if( !NoEdit )   //변경된 내용이 있다면
            {
                FormYesNo frm = new FormYesNo();
                DialogResult re = frm.ShowDialog();
                if (re == DialogResult.Cancel) e.Cancel = true;    폼 닫기 취소 아래코드 실행 안되고 return
                if (re == DialogResult.Yes) btSave_Click(null, null);
            }
        }//func


        private void btSerch_Click(object sender, EventArgs e)
        {
            int rowIndex = 0;   // Row Index   첫번째 Row                      

            if (txtSerch.Text == "" ) return; // 빈값이면 return

            foreach (DataRow row in dt.Rows) // Row를 순차적으로 하나씩 
            {                                // 가져옴
                foreach(object j in dt.Columns )  // Column을 하나씩
                   if (row[ j.ToString()  ].ToString() == txtSerch.Text)
                   {
                    dataGrid.CurrentCell = dataGrid.Rows[rowIndex].Cells[0];
                        // 찾은 Row를 현재 Row로 설정 Rows에는 없어 Cell단위로 설정
                        // 또는 dataGrid.Rows[rowIndex].Cells[1].Selected = true;

                        laIndex.Text = "Index : "+rowIndex.ToString(); 
                        return;   //바로 return으로 검색종료
                   }// 찾으면 실행
                rowIndex++;
            }// Row를 하나씩 검색
            laIndex.Text = "Not Found"; // 못 찾으면 실행
        } //func



        private void btSave_Click(object sender, EventArgs e)
        {
            DataSet ds2 = new DataSet();

            string filePath;           
            saveFileDialog.InitialDirectory = wf;   
            saveFileDialog.FileName = "*.xml";
            saveFileDialog.Filter = "xml files (*.xml)|*.xml|All files (*.*)|*.*";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                filePath = saveFileDialog.FileName;
                ds2.ReadXml(df);//변경전XML 읽기
                ds2.WriteXml(filePath.Substring(0, filePath.Length - 3) + "bak"); 
                ds.WriteXml(filePath);
                df = filePath;
                wf = filePath.Substring(0, filePath.Length - 4);      //파일명 없는 경로          
                this.Text = "XML Grid Manager - " + df.Substring(df.LastIndexOf('\\') + 1);
                NoEdit = true;
                ControlEnable();
            }
        }//func


        private void menuRunExe_Click(object sender, EventArgs e)
        {
            FormMakeCaution frm=new FormMakeCaution();
            if (frm.ShowDialog() == DialogResult.Cancel) return;
            System.Diagnostics.Process exe = new Process();
            exe.StartInfo.FileName = "XmlDataBaseManager.exe";
            exe.StartInfo.WorkingDirectory = Application.StartupPath+ "\\XmlDataBaseManager";   
            exe.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            exe.Start();
            exe.WaitForExit();   // 외부프로그램 종료까지 기다림
        }//fuc
      


       private void dataGrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            // 행헤더 열영역에 행번호를 보여주기 위해 장방형으로 처리
            Rectangle rect = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y,
                                           dataGrid.RowHeadersWidth - 4, e.RowBounds.Height);

            // 위에서 생성된 장방형내에 행번호를 보여주고 폰트색상 및 배경을 설정
            TextRenderer.DrawText(e.Graphics, (e.RowIndex).ToString(),
                                  dataGrid.RowHeadersDefaultCellStyle.Font, rect,
                                  dataGrid.RowHeadersDefaultCellStyle.ForeColor,
                                  TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }// func