티스토리 뷰





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

namespace GridSample
{
    public partial class Form1 : Form
    {
        private DataSet ourDataSet;
        private DataTable myDataTable;
        private BindingSource ourBindingSource;

        public Form1()
        {
            InitializeComponent();
            InitializeMyComponent();
        }

        private void InitializeMyComponent()
        {
            ourDataSet = new DataSet();
            myDataTable = new DataTable("Person");
            ourBindingSource = new BindingSource();

            myDataTable.Columns.Add("Name", typeof(string));
            myDataTable.Columns.Add("Age", typeof(int));
            myDataTable.Columns.Add("Man", typeof(bool));

            myDataTable.Columns["Name"].AllowDBNull = false;
            myDataTable.Columns["Name"].Unique = true;

            ourDataSet.Tables.Add(myDataTable);

            ourDataSet.ReadXml("Person.xml");

            ourBindingSource.DataSource = ourDataSet;
            ourBindingSource.DataMember = "Person";
            this.dataGridView1.DataSource = ourBindingSource;
        }

        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            this.Validate();
            ourBindingSource.EndEdit();
            ourDataSet.WriteXml("Person.xml");
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView1.Rows[e.RowIndex].ErrorText = String.Empty;
        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            dataGridView1.Rows[e.RowIndex].ErrorText = e.Exception.Message;
            e.Cancel = true;
        }
    }
}





Person.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Person>
    <Name>Paul</Name>
    <Age>40</Age>
    <Man>true</Man>
  </Person>
  <Person>
    <Name>Jane</Name>
    <Age>41</Age>
    <Man>false</Man>
  </Person>
  <Person>
    <Name>Joe</Name>
    <Age>36</Age>
    <Man>true</Man>
  </Person>
  <Person>
    <Name>Lora</Name>
    <Age>21</Age>
  </Person>
</NewDataSet>



공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함