using System;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace Lights01
{
public class DataViewForm : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
DataTable dt; // Таблица.
DataColumn c1, c2; // Столбцы таблцы.
DataRow dr; // Строка таблицы.
DataView dv; // Вьюер таблицы.
DataRowView rv; // Вьюер строки таблицы.
int currentCounter; // Счетчик текущей строки для вьюера таблицы.
private System.Windows.Forms.DataGrid dG;
private System.Windows.Forms.DataGrid dGforTable;
private System.Windows.Forms.Button buttPrev;
private System.Windows.Forms.Button buttFirst;
private System.Windows.Forms.Button buttLast;
private System.Windows.Forms.Button buttonFind;
private System.Windows.Forms.TextBox demoTextBox;
private System.Windows.Forms.TextBox findTextBox;
private System.Windows.Forms.Button buttonAdd;
private System.Windows.Forms.Button buttonAcc;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button buttNext;
public DataViewForm()
{
InitializeComponent();
CreateTable();
dG.DataSource = dv;
dGforTable.DataSource = dt;
currentCounter = 0;
rv = dv[currentCounter];
demoTextBox.Text = rv["Item"].ToString();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
// Required method for Designer support – do not modify
// the contents of this method with the code editor.
private void InitializeComponent()
{
this.dG = new System.Windows.Forms.DataGrid();
this.demoTextBox = new System.Windows.Forms.TextBox();
this.buttPrev = new System.Windows.Forms.Button();
this.buttNext = new System.Windows.Forms.Button();
this.buttFirst = new System.Windows.Forms.Button();
this.buttLast = new System.Windows.Forms.Button();
this.findTextBox = new System.Windows.Forms.TextBox();
this.buttonFind = new System.Windows.Forms.Button();
this.buttonAdd = new System.Windows.Forms.Button();
this.dGforTable = new System.Windows.Forms.DataGrid();
this.buttonAcc = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
((System.ComponentModel.ISupportInitialize)(this.dG)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dGforTable)).BeginInit();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// dG
//
this.dG.DataMember = "";
this.dG.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dG.Location = new System.Drawing.Point(8, 80);
this.dG.Name = "dG";
this.dG.Size = new System.Drawing.Size(280, 128);
this.dG.TabIndex = 0;
this.dG.MouseDown +=
new System.Windows.Forms.MouseEventHandler(this.dG_MouseDown);
//
// demoTextBox
//
this.demoTextBox.Location = new System.Drawing.Point(152, 48);
this.demoTextBox.Name = "demoTextBox";
this.demoTextBox.Size = new System.Drawing.Size(128, 20);
this.demoTextBox.TabIndex = 1;
this.demoTextBox.Text = "";
//
// buttPrev
//
this.buttPrev.Location = new System.Drawing.Point(189, 16);
this.buttPrev.Name = "buttPrev";
this.buttPrev.Size = new System.Drawing.Size(25, 23);
this.buttPrev.TabIndex = 2;
this.buttPrev.Text = "<–";
this.buttPrev.Click += new System.EventHandler(this.buttPrev_Click);
//
// buttNext
//
this.buttNext.Location = new System.Drawing.Point(221, 16);
this.buttNext.Name = "buttNext";
this.buttNext.Size = new System.Drawing.Size(25, 23);
this.buttNext.TabIndex = 3;
this.buttNext.Text = "–>";
this.buttNext.Click += new System.EventHandler(this.buttNext_Click);
//
// buttFirst
//
this.buttFirst.Location = new System.Drawing.Point(157, 16);
this.buttFirst.Name = "buttFirst";
this.buttFirst.Size = new System.Drawing.Size(25, 23);
this.buttFirst.TabIndex = 4;
this.buttFirst.Text = "<<";
this.buttFirst.Click += new System.EventHandler(this.buttFirst_Click);
//
// buttLast
//
this.buttLast.Location = new System.Drawing.Point(253, 16);
this.buttLast.Name = "buttLast";
this.buttLast.Size = new System.Drawing.Size(25, 23);
this.buttLast.TabIndex = 5;
this.buttLast.Text = ">>";
this.buttLast.Click += new System.EventHandler(this.buttLast_Click);
//
// findTextBox
//
this.findTextBox.Location = new System.Drawing.Point(8, 48);
this.findTextBox.Name = "findTextBox";
this.findTextBox.Size = new System.Drawing.Size(128, 20);
this.findTextBox.TabIndex = 6;
this.findTextBox.Text = "";
//
// buttonFind
//
this.buttonFind.Location = new System.Drawing.Point(88, 16);
this.buttonFind.Name = "buttonFind";
this.buttonFind.Size = new System.Drawing.Size(48, 23);
this.buttonFind.TabIndex = 7;
this.buttonFind.Text = "Find";
this.buttonFind.Click += new System.EventHandler(this.buttonFind_Click);
//
// buttonAdd
//
this.buttonAdd.Location = new System.Drawing.Point(8, 16);
this.buttonAdd.Name = "buttonAdd";
this.buttonAdd.Size = new System.Drawing.Size(40, 23);
this.buttonAdd.TabIndex = 8;
this.buttonAdd.Text = "Add";
this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
//
// dGforTable
//
this.dGforTable.DataMember = "";
this.dGforTable.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dGforTable.Location = new System.Drawing.Point(8, 24);
this.dGforTable.Name = "dGforTable";
this.dGforTable.Size = new System.Drawing.Size(272, 120);
this.dGforTable.TabIndex = 9;
//
// buttonAcc
//
this.buttonAcc.Location = new System.Drawing.Point(8, 152);
this.buttonAcc.Name = "buttonAcc";
this.buttonAcc.Size = new System.Drawing.Size(40, 23);
this.buttonAcc.TabIndex = 10;
this.buttonAcc.Text = "Acc";
this.buttonAcc.Click += new System.EventHandler(this.buttonAcc_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.buttonAcc);
this.groupBox1.Controls.Add(this.dGforTable);
this.groupBox1.Location = new System.Drawing.Point(6, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(298, 184);
this.groupBox1.TabIndex = 11;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Таблица как она есть ";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.buttPrev);
this.groupBox2.Controls.Add(this.buttonFind);
this.groupBox2.Controls.Add(this.buttFirst);
this.groupBox2.Controls.Add(this.buttLast);
this.groupBox2.Controls.Add(this.demoTextBox);
this.groupBox2.Controls.Add(this.buttNext);
this.groupBox2.Controls.Add(this.dG);
this.groupBox2.Controls.Add(this.buttonAdd);
this.groupBox2.Controls.Add(this.findTextBox);
this.groupBox2.Location = new System.Drawing.Point(8, 200);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(296, 216);
this.groupBox2.TabIndex = 12;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Вид через вьюер";
//
// DataViewForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(312, 421);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Name = "DataViewForm";
this.Text = "DataViewForm";
((System.ComponentModel.ISupportInitialize)(this.dG)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dGforTable)).EndInit();
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void CreateTable()
{
// Создается таблица.
dt = new DataTable("Items");
// Столбцы таблицы...
// Имя первого столбца – id, тип значения – System.Int32.
c1 = new DataColumn("id", Type.GetType("System.Int32"));
c1.AutoIncrement=true;
// Имя второго столбца – Item, тип значения – System.Int32.
c2 = new DataColumn("Item", Type.GetType("System.Int32"));
// К таблице добавляются объекты-столбцы...
dt.Columns.Add(c1);
dt.Columns.Add(c2);
// А вот массив столбцов (здесь он из одного элемента)
// для организации первичного ключа (множества первичных ключей).
DataColumn[] keyCol= new DataColumn[1];
// И вот, собственно, как в таблице задается множество первичных ключей.
keyCol[0]= c1;
// Свойству объекта t передается массив, содержащий столбцы, которые
// формируемая таблица t будет воспринимать как первичные ключи.
dt.PrimaryKey=keyCol;
// В таблицу добавляется 10 rows.
for(int i = 0; i <10;i++)
{
dr=dt.NewRow();
dr["Item"]= i;
dt.Rows.Add(dr);
}
// Принять изменения.
// Так производится обновление таблицы.
// Сведения о новых изменениях и добавлениях будут фиксироваться
// вплоть до нового обновления.
dt.AcceptChanges();
// Здесь мы применим специализированный конструктор, который
// задаст значения свойств Table, RowFilter, Sort, RowStateFilter
// объекта DataView в двух операторах кода...
//dv = new DataView(dt); // Вместо этого...
// Определение того, что доступно через объект - представитель DataView.
// Задавать можно в виде битовой суммы значений. И не все значения сразу!
// Сумма всех значений – противоречивое сочетание!
// А можно ли делать это по отдельности?
DataViewRowState dvrs = DataViewRowState.Added |
DataViewRowState.CurrentRows |
DataViewRowState.Deleted |
DataViewRowState.ModifiedCurrent |
//DataViewRowState.ModifiedOriginal |
//DataViewRowState.OriginalRows |
//DataViewRowState.None |
// Записи не отображаются.
DataViewRowState.Unchanged;
// Вот такое хитрое объявление...
// Таблица,
// | значение, относительно которого проводится сортировка,
// | |
// | | имя столбца, значения которого сортируются,
// | | |
// | | | составленное значение DataViewRowState.
// | | | |
dv = new DataView(dt, "", "Item", dvrs);
}
private void buttNext_Click(object sender, System.EventArgs e)
{
if (currentCounter+1 < dv.Count) currentCounter++;
rv = dv[currentCounter];
demoTextBox.Text = rv["Item"].ToString();
}
private void buttPrev_Click(object sender, System.EventArgs e)
{
if (currentCounter–1 >= 0) currentCounter––;
rv = dv[currentCounter];
demoTextBox.Text = rv["Item"].ToString();
}
private void buttFirst_Click(object sender, System.EventArgs e)
{
currentCounter = 0;
rv = dv[currentCounter];
demoTextBox.Text = rv["Item"].ToString();
}
private void buttLast_Click(object sender, System.EventArgs e)
{
currentCounter =dv.Count – 1;
rv = dv[currentCounter];
demoTextBox.Text = rv["Item"].ToString();
}
private void dG_MouseDown
(object sender, System.Windows.Forms.MouseEventArgs e)
{
currentCounter = dG.CurrentRowIndex;
rv = dv[currentCounter];
demoTextBox.Text = rv["Item"].ToString();
}
// Реализация поиска требует специального опеделения порядка
// сортировки строк, который должен задаваться в конструкторе.
private void buttonFind_Click(object sender, System.EventArgs e)
{
int findIndex = –1;
// Сначала проверяем строку на соответствие формату отыскиваемого
// значения.
// В нашем случае строка должна преобразовываться в целочисленное
// значение.
try
{
int.Parse(findTextBox.Text);
}
catch
{
findTextBox.Text = "Неправильно задан номер...";
return;
}
findIndex = dv.Find(findTextBox.Text);
if (findIndex >= 0)
{
currentCounter = findIndex;
rv = dv[currentCounter];
demoTextBox.Text = rv["Item"].ToString();
}
else
{
findTextBox.Text = "Не нашли.";
}
}
private void buttonAdd_Click(object sender, System.EventArgs e)
{
// При создании новой записи средствами вьюера таблицы,
// связанный с ним вьюер строки переходит в состояние rv.IsNew.
// При этом в действиях этих объектов есть своя логика.
// И если туда не вмешиваться, при создании очередной записи
// предыдущая запись считается принятой и включается в таблицу
// автоматически.
// Контролируя состояния вьюера строки (rv.IsEdit || rv.IsNew),
// мы можем предотвратить процесс последовательного автоматического
// обновления таблицы. Все под контролем.
if (rv.IsEdit || rv.IsNew) return;
rv = dv.AddNew();
rv["Item"] = dv.Count–1;
}
private void buttonAcc_Click(object sender, System.EventArgs e)
{
// И вот мы вмешались в процесс.
// Добавление новой записи в таблицу становится возможным лишь
// после явного завершения редактирования предыдущей записи.
// Без этого попытки создания новой записи блокируются.
// Завершить редактирование.
rv.EndEdit();
// Принять изменения.
// Так производится обновление таблицы.
// Сведения о новых изменениях и добавлениях будут фиксироваться
// вплоть до нового обновления.
dt.AcceptChanges();
}
}
} |
Листинг 18.6. |
| Закрыть окно |