Here is a C# program that you can use to try a DataTable class
In this program, I used buttons and keyboard events to trigger variuos operation on the DataTable. There is one button to save a DataTable as an XML file (by saving the DataSet that contains the table). There is a DataGrid that let's you view and edit the table. I want to allow the user to undo up to 10 changes made in the DataTable, but that part is unfinished (not much was done). Also I wanted to selectively delete the first row in the table when it is in alphabeticall order. Any one can help me with these two problems? I am planning it out using what is available in the class such as the event triggers (DataTabele.RowChanged), and the rollback functions (DataTable.RejectChanges), plus any costum implemation I can think of to do this problem.
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace NoobyDataTable
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid SettingsView;
private System.Data.DataTable tableSettings;
private System.Windows.Forms.Button butSaveTable;
private System.Windows.Forms.TextBox textBoxTableName;
private System.Windows.Forms.Button butDeleteTop;
private System.Windows.Forms.Button butLoadDefaults;
private System.Windows.Forms.Button butSelectRow;
private System.Windows.Forms.Button butUndoLast;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
/*
* Public Methods
* DataTable.AcceptChanges
* DataTable.RejectChanges
* DataTable.Select
* DataTable.NewRow
* DataTable.DataSet
*
* Public Events:
* DataTable.RowChanged
* DataTable.RowDeleted
* */
//Create a new table named Settings
this.tableSettings = new System.Data.DataTable("Settings");
//Create a column named "Name" for the table
DataColumn columnName = new System.Data.DataColumn("Name", System.Type.GetType("System.String"));
//Set the column to be writable, even though it has this value by default
columnName.ReadOnly = false;
//Add the column "Name" to the table "Settings"
this.tableSettings.Columns.Add(columnName);
//Create another column named "Value" which stores 32 bit integers
DataColumn columnValue = new System.Data.DataColumn("Value", System.Type.GetType("System.Int32"));
//Add the next column to the table
this.tableSettings.Columns.Add(columnValue);
//Set the first column "Name" as the primary key column for the table. This will allow only unique "Names" in the table
DataColumn[] columnsPrimaryKeys = new DataColumn[1];
columnsPrimaryKeys[0] = this.tableSettings.Columns["Name"];
this.tableSettings.PrimaryKey = columnsPrimaryKeys;
//Use the DataView property of the table to set the display of the table. Here I set the rows to odered by ascending names.
if(this.tableSettings.DefaultView != null)
//A DataView must be present, but in case the Framework changes, I check it first
this.tableSettings.DefaultView.Sort = "Name ASC";
//Bind the table to the DataGrid control which I had named SettingsView
this.SettingsView.SetDataBinding(tableSettings, "");
//I want the same event handler, implemented near bottom, for two similar events
this.tableSettings.RowChanged += new DataRowChangeEventHandler( Row_Changed );
//this.tableSettings.RowDeleted += new DataRowChangeEventHandler( Row_Changed );
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SettingsView = new System.Windows.Forms.DataGrid();
this.butSaveTable = new System.Windows.Forms.Button();
this.textBoxTableName = new System.Windows.Forms.TextBox();
this.butDeleteTop = new System.Windows.Forms.Button();
this.butLoadDefaults = new System.Windows.Forms.Button();
this.butSelectRow = new System.Windows.Forms.Button();
this.butUndoLast = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.SettingsView)).BeginInit();
this.SuspendLayout();
//
// SettingsView
//
this.SettingsView.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.SettingsView.BackgroundColor = System.Drawing.Color.LightGray;
this.SettingsView.DataMember = "";
this.SettingsView.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.SettingsView.Location = new System.Drawing.Point(16, 96);
this.SettingsView.Name = "SettingsView";
this.SettingsView.Size = new System.Drawing.Size(264, 264);
this.SettingsView.TabIndex = 0;
//
// butSaveTable
//
this.butSaveTable.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
this.butSaveTable.Location = new System.Drawing.Point(16, 376);
this.butSaveTable.Name = "butSaveTable";
this.butSaveTable.TabIndex = 1;
this.butSaveTable.Text = "Save Table";
this.butSaveTable.Click += new System.EventHandler(this.butSaveTable_Click);
//
// textBoxTableName
//
this.textBoxTableName.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.textBoxTableName.Location = new System.Drawing.Point(104, 379);
this.textBoxTableName.Name = "textBoxTableName";
this.textBoxTableName.Size = new System.Drawing.Size(176, 20);
this.textBoxTableName.TabIndex = 2;
this.textBoxTableName.Text = "Settings";
this.textBoxTableName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBoxTableName_KeyDown);
this.textBoxTableName.TextChanged += new System.EventHandler(this.textBoxTableName_TextChanged);
//
// butDeleteTop
//
this.butDeleteTop.Location = new System.Drawing.Point(28, 56);
this.butDeleteTop.Name = "butDeleteTop";
this.butDeleteTop.Size = new System.Drawing.Size(104, 24);
this.butDeleteTop.TabIndex = 3;
this.butDeleteTop.Text = "Delete Top Row";
this.butDeleteTop.Click += new System.EventHandler(this.butDeleteTop_Click);
//
// butLoadDefaults
//
this.butLoadDefaults.Location = new System.Drawing.Point(28, 16);
this.butLoadDefaults.Name = "butLoadDefaults";
this.butLoadDefaults.Size = new System.Drawing.Size(104, 24);
this.butLoadDefaults.TabIndex = 3;
this.butLoadDefaults.Text = "Load Default Row";
this.butLoadDefaults.Click += new System.EventHandler(this.butLoadDefaults_Click);
//
// butSelectRow
//
this.butSelectRow.Location = new System.Drawing.Point(164, 16);
this.butSelectRow.Name = "butSelectRow";
this.butSelectRow.Size = new System.Drawing.Size(104, 24);
this.butSelectRow.TabIndex = 3;
this.butSelectRow.Text = "Select \'Var\' Row";
this.butSelectRow.Click += new System.EventHandler(this.butSelectRow_Click);
//
// butUndoLast
//
this.butUndoLast.Location = new System.Drawing.Point(164, 56);
this.butUndoLast.Name = "butUndoLast";
this.butUndoLast.Size = new System.Drawing.Size(104, 24);
this.butUndoLast.TabIndex = 3;
this.butUndoLast.Text = "Undo Last Row";
this.butUndoLast.Click += new System.EventHandler(this.butUndoLast_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(296, 413);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.butDeleteTop,
this.textBoxTableName,
this.butSaveTable,
this.SettingsView,
this.butLoadDefaults,
this.butSelectRow,
this.butUndoLast});
this.Name = "Form1";
this.Text = "View a DataTable";
((System.ComponentModel.ISupportInitialize)(this.SettingsView)).EndInit();
this.ResumeLayout(false);
}
#endregion
contiuned to next post