/* the DataTable.Copy() and Clone() methods */
        private void button5_Click(object sender, EventArgs e)
        {
            DataTable temp = table.Copy();

            TableBox.Display(temp);

            temp = table.Clone();
            TableBox.Display(temp);
        }
Beispiel #2
0
        /*  Demostrates the DataTable.GetChanges() method  */
        private void button1_Click(object sender, EventArgs e)
        {
            DataTable temp = CreateTempTable();

            /* display the table */
            temp.AcceptChanges();
            TableBox.Display(temp);



            /* display added rows */
            temp.Rows.Add(111);

            DataTable Added = temp.GetChanges(DataRowState.Added);

            if (Added != null)
            {
                Added.TableName = "Added";
                TableBox.Display(Added);
            }

            temp.AcceptChanges();


            /* display modified rows */
            temp.Rows[0]["ID"] = 123;
            temp.Rows[2]["ID"] = 456;


            DataTable Modified = temp.GetChanges(DataRowState.Modified);

            if (Modified != null)
            {
                Modified.TableName = "Modified";
                TableBox.Display(Modified);
            }

            temp.AcceptChanges();


            /* display deleted rows */
            temp.Rows[2].Delete();      // DataRow.Delete() marks the row for deletion
            temp.Rows[4].Delete();
            temp.Rows.RemoveAt(3);      // WARNING: DataTable.Rows.Remove() and RemoveAt() delete the row and call AcceptChanges()


            DataTable Deleted = temp.GetChanges(DataRowState.Deleted);

            if (Deleted != null)
            {
                Deleted.TableName = "Deleted";
                Deleted.RejectChanges();    // if RejectChanges() is omitted the Deleted table does not display any row
                TableBox.Display(Deleted);
            }
        }
        /* saving and loading a table to/from an xml file */
        private void button4_Click(object sender, EventArgs e)
        {
            string FileName = Path.GetFullPath(@"..\..\Table.XML");

            table.WriteXml(FileName, XmlWriteMode.WriteSchema);

            DataTable temp = new DataTable();

            temp.ReadXml(FileName);

            TableBox.Display(temp);
        }