Windows Form File /DBCommander/DataPanel.cs (C#)
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace YariSoft.DBCommander
{
public class DataPanel : System.Windows.Forms.UserControl
{
#region Events
public delegate void OnGridClickEventHandler(object owner, object sender, System.EventArgs e);
public delegate void OnNeedSelectionMasksEventHandler( ref ArrayList Selections );
public delegate void OnChangeDetailLevelEventHandler();
public delegate void OnNeedSetGridStyleEventHandler( object Sender, DataGridTableStyle style );
public delegate void OnColumnResizeEventHandler( object Sender, DataGrid.HitTestInfo Info );
public event OnGridClickEventHandler OnGridClick = null;
public event OnNeedSelectionMasksEventHandler OnNeedSelectionMasks = null;
public event OnChangeDetailLevelEventHandler OnChangeDetailLevel = null;
public event OnNeedSetGridStyleEventHandler OnNeedSetGridStyle = null;
public event OnColumnResizeEventHandler OnColumnResize = null;
#endregion
#region Local variables
private DataPanelManager dataManager = null;
private System.Windows.Forms.Panel bottomPanel;
private System.Windows.Forms.Panel topPanel;
private YariSoft.Windows.Conrols.YDataGrid grid;
private System.Windows.Forms.Label serverLabel;
private System.Windows.Forms.ComboBox tablesCombo;
private System.Windows.Forms.DataGridTableStyle dataGridTablesInternal;
private System.Windows.Forms.DataGridTextBoxColumn dataGridNameColumn;
private System.Windows.Forms.DataGridTextBoxColumn dataGridDateColumn;
private System.Windows.Forms.DataGridTextBoxColumn dataGridOwnerColumn;
private System.Windows.Forms.ComboBox tablesCombo2;
private System.Windows.Forms.Label rowsLabel;
private System.ComponentModel.IContainer components = null;
#endregion
#region Properties
public System.Data.OleDb.OleDbConnection Connection
{
get{ return this.dataManager.Connection; }
}
public bool Active
{
set{
if( value ){
this.topPanel.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.serverLabel.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
} else {
this.topPanel.BackColor = System.Drawing.SystemColors.Control;
this.serverLabel.ForeColor = System.Drawing.SystemColors.ControlText;
}
this.grid.CaptionBackColor = this.topPanel.BackColor;
}
}
public YariSoft.DBCommander.DetailLevel DetailLevel{
get{ return this.dataManager.DetailLevel; }
}
public YariSoft.Windows.Conrols.YDataGrid PanelGrid {
get{ return this.grid; }
}
public DataPanelManager Manager {
get{ return this.dataManager; }
}
public ArrayList SelectedRows {
get{
ArrayList result = null;
DataView view = this.GetActiveDataView( this.grid );
if( view != null ){
result = new ArrayList();
for( int i = 0; i < view.Count; i++ ){
if( this.grid.IsSelected(i) ){
if( this.DetailLevel == DetailLevel.DataLevel ){
result.Add ( i );
} else if ( this.DetailLevel == DetailLevel.TableLevel ) {
result.Add( view[i]["TABLE_NAME"] );
}
}
}
if( result.Count == 0 && (( CurrencyManager )BindingContext[ this.grid.DataSource, this.grid.DataMember ]).Position >= 0 ){
if( this.DetailLevel == DetailLevel.DataLevel ){
result.Add( (( CurrencyManager )BindingContext[ this.grid.DataSource, this.grid.DataMember ]).Position );
} else if ( this.DetailLevel == DetailLevel.TableLevel ) {
result.Add( view[(( CurrencyManager )BindingContext[ this.grid.DataSource, this.grid.DataMember ]).Position]["TABLE_NAME"] );
}
}
}
return result;
}
}
public bool IdentityOff
{
get{ return this.dataManager.IdentityOff; }
set{ this.dataManager.IdentityOff = value; }
}
#endregion
#region Constructor/Destructor
public DataPanel()
{
InitializeComponent();
this.dataGridNameColumn.TextBox.DoubleClick += new EventHandler( this.DownClick );
this.dataGridDateColumn.TextBox.DoubleClick += new EventHandler( this.DownClick );
this.dataGridOwnerColumn.TextBox.DoubleClick += new EventHandler( this.DownClick );
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
this.dataManager.Dispose();
}
base.Dispose( disposing );
}
#endregion
#region Component 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.bottomPanel = new System.Windows.Forms.Panel();
this.rowsLabel = new System.Windows.Forms.Label();
this.grid = new YariSoft.Windows.Conrols.YDataGrid();
this.dataGridTablesInternal = new System.Windows.Forms.DataGridTableStyle();
this.dataGridNameColumn = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridDateColumn = new System.Windows.Forms.DataGridTextBoxColumn();
this.dataGridOwnerColumn = new System.Windows.Forms.DataGridTextBoxColumn();
this.topPanel = new System.Windows.Forms.Panel();
this.tablesCombo = new System.Windows.Forms.ComboBox();
this.serverLabel = new System.Windows.Forms.Label();
this.tablesCombo2 = new System.Windows.Forms.ComboBox();
this.bottomPanel.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.grid)).BeginInit();
this.topPanel.SuspendLayout();
this.SuspendLayout();
//
// bottomPanel
//
this.bottomPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.bottomPanel.Controls.AddRange(new System.Windows.Forms.Control[] {
this.rowsLabel});
this.bottomPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
this.bottomPanel.Location = new System.Drawing.Point(0, 364);
this.bottomPanel.Name = "bottomPanel";
this.bottomPanel.Size = new System.Drawing.Size(448, 20);
this.bottomPanel.TabIndex = 2;
//
// rowsLabel
//
this.rowsLabel.AutoSize = true;
this.rowsLabel.Dock = System.Windows.Forms.DockStyle.Left;
this.rowsLabel.Font = new System.Drawing.Font("Tahoma", 8.25F);
this.rowsLabel.Name = "rowsLabel";
this.rowsLabel.Size = new System.Drawing.Size(38, 14);
this.rowsLabel.TabIndex = 0;
this.rowsLabel.Text = "Rows: ";
//
// grid
//
this.grid.BackgroundColor = System.Drawing.SystemColors.Window;
this.grid.CaptionFont = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Bold);
this.grid.DataMember = "";
this.grid.Dock = System.Windows.Forms.DockStyle.Fill;
this.grid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.grid.Location = new System.Drawing.Point(0, 24);
this.grid.Name = "grid";
this.grid.Size = new System.Drawing.Size(448, 340);
this.grid.TabIndex = 1;
this.grid.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
this.dataGridTablesInternal});
this.grid.MouseDown += new System.Windows.Forms.MouseEventHandler(this.grid_MouseDown);
this.grid.DataSourceChanged += new System.EventHandler(this.grid_DataSourceChanged);
this.grid.Navigate += new System.Windows.Forms.NavigateEventHandler(this.grid_Navigate);
this.grid.DoubleClick += new System.EventHandler(this.DownClick);
this.grid.Click += new System.EventHandler(this.grid_Click);
this.grid.MouseMove += new System.Windows.Forms.MouseEventHandler(this.grid_MouseMove);
this.grid.CurrentCellChanged += new System.EventHandler(this.grid_CurrentCellChanged);
this.grid.YOnKeyDown += new YariSoft.Windows.Conrols.YDataGrid.YKeyEventHandler(this.grid_YOnKeyDown);
//
// dataGridTablesInternal
//
this.dataGridTablesInternal.DataGrid = this.grid;
this.dataGridTablesInternal.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
this.dataGridNameColumn,
this.dataGridDateColumn,
this.dataGridOwnerColumn});
this.dataGridTablesInternal.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGridTablesInternal.MappingName = "__YS_Tables_Internal";
this.dataGridTablesInternal.ReadOnly = true;
//
// dataGridNameColumn
//
this.dataGridNameColumn.Format = "";
this.dataGridNameColumn.FormatInfo = null;
this.dataGridNameColumn.HeaderText = "Name";
this.dataGridNameColumn.MappingName = "Table_Name";
this.dataGridNameColumn.Width = 200;
//
// dataGridDateColumn
//
this.dataGridDateColumn.Format = "";
this.dataGridDateColumn.FormatInfo = null;
this.dataGridDateColumn.HeaderText = "Create Date";
this.dataGridDateColumn.MappingName = "Date_Created";
this.dataGridDateColumn.ReadOnly = true;
this.dataGridDateColumn.Width = 150;
//
// dataGridOwnerColumn
//
this.dataGridOwnerColumn.Format = "";
this.dataGridOwnerColumn.FormatInfo = null;
this.dataGridOwnerColumn.HeaderText = "Owner";
this.dataGridOwnerColumn.MappingName = "Table_Schema";
this.dataGridOwnerColumn.ReadOnly = true;
this.dataGridOwnerColumn.Width = 75;
//
// topPanel
//
this.topPanel.BackColor = System.Drawing.SystemColors.Control;
this.topPanel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.topPanel.Controls.AddRange(new System.Windows.Forms.Control[] {
this.tablesCombo,
this.serverLabel});
this.topPanel.Dock = System.Windows.Forms.DockStyle.Top;
this.topPanel.Name = "topPanel";
this.topPanel.Size = new System.Drawing.Size(448, 24);
this.topPanel.TabIndex = 0;
this.topPanel.Visible = false;
this.topPanel.Click += new System.EventHandler(this.grid_Click);
//
// tablesCombo
//
this.tablesCombo.Dock = System.Windows.Forms.DockStyle.Left;
this.tablesCombo.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.tablesCombo.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
this.tablesCombo.Location = new System.Drawing.Point(120, 0);
this.tablesCombo.Name = "tablesCombo";
this.tablesCombo.Size = new System.Drawing.Size(200, 21);
this.tablesCombo.TabIndex = 1;
this.tablesCombo.TabStop = false;
this.tablesCombo.KeyUp += new System.Windows.Forms.KeyEventHandler(this.tablesCombo_KeyUp);
this.tablesCombo.SelectedIndexChanged += new System.EventHandler(this.tablesCombo_SelectedIndexChanged);
//
// serverLabel
//
this.serverLabel.Dock = System.Windows.Forms.DockStyle.Left;
this.serverLabel.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
this.serverLabel.Name = "serverLabel";
this.serverLabel.Size = new System.Drawing.Size(120, 20);
this.serverLabel.TabIndex = 0;
this.serverLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.serverLabel.Click += new System.EventHandler(this.grid_Click);
//
// tablesCombo2
//
this.tablesCombo2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.tablesCombo2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F);
this.tablesCombo2.Location = new System.Drawing.Point(120, 1);
this.tablesCombo2.Name = "tablesCombo2";
this.tablesCombo2.Size = new System.Drawing.Size(200, 21);
this.tablesCombo2.TabIndex = 5;
this.tablesCombo2.TabStop = false;
this.tablesCombo2.KeyUp += new System.Windows.Forms.KeyEventHandler(this.tablesCombo_KeyUp);
this.tablesCombo2.SelectedIndexChanged += new System.EventHandler(this.tablesCombo_SelectedIndexChanged);
//
// DataPanel
//
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.tablesCombo2,
this.grid,
this.topPanel,
this.bottomPanel});
this.Name = "DataPanel";
this.Size = new System.Drawing.Size(448, 384);
this.bottomPanel.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.grid)).EndInit();
this.topPanel.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
#region Public functions
public bool Init( string ConnectionString )
{
if( this.dataManager != null ){
this.dataManager.Dispose();
}
this.dataManager = new DataPanelManager( ConnectionString );
this.RefreshDataBaseData();
return true;
}
public bool PrepareConnection ()
{
if( this.dataManager.PrepareConnection() ){
this.RefreshDataBaseData();
return true;
}
return false;
}
public void MoveDown()
{
if( this.dataManager.DetailLevel < YariSoft.DBCommander.DetailLevel.DataLevel ){
this.Cursor = Cursors.WaitCursor;
this.dataManager.MoveDown();
this.FillData();
this.Cursor = Cursors.Default;
if( this.OnChangeDetailLevel != null ){
this.OnChangeDetailLevel();
}
}
}
public void MoveUp()
{
if( this.dataManager.DetailLevel > YariSoft.DBCommander.DetailLevel.TableLevel ){
bool status= true;
this.Cursor = Cursors.WaitCursor;
if( status ){
this.dataManager.MoveUp();
this.FillData();
}
this.Cursor = Cursors.Default;
if( this.OnChangeDetailLevel != null ){
this.OnChangeDetailLevel();
}
}
}
private DataView PrepareOperation( DataPanel Panel )
{
DataView view = this.GetActiveDataView( Panel.PanelGrid );
if( Panel.PanelGrid.DataMember == "" ){
Panel.BackgroundImage = Panel.PanelGrid.GetGridImage();
Panel.PanelGrid.DataSource = null;
Panel.PanelGrid.Visible = false;
}
return view;
}
public bool CopyData( DataPanel Destination )
{
bool status = false;
switch ( this.dataManager.DetailLevel ){
case DetailLevel.TableLevel:
status = this.dataManager.CopyData ( null, null, this.SelectedRows, Destination.Manager );
Destination.RefreshDataBaseData();
break;
case DetailLevel.DataLevel:
DataView view = this.PrepareOperation ( Destination );
status = this.dataManager.CopyData ( this.GetActiveDataView ( this.grid ),view, this.SelectedRows, Destination.Manager );
if( Destination.PanelGrid.DataSource == null ){
Destination.PanelGrid.DataSource = view;
Destination.PanelGrid.Visible = true;
}
break;
}
this.ScrollToRow ( Destination.PanelGrid );
this.CheckDataState( Destination );
return status;
}
public bool DeleteData()
{
bool status = false;
ArrayList selected = this.SelectedRows;
switch ( this.dataManager.DetailLevel ){
case DetailLevel.TableLevel:
status = this.dataManager.DeleteData( null, selected );
this.RefreshDataBaseData();
break;
case DetailLevel.DataLevel:
DataView view = this.PrepareOperation ( this );
status = this.dataManager.DeleteData ( view, selected );
if( this.grid.DataSource == null ){
this.grid.DataSource = view;
this.grid.Visible = true;
}
break;
}
this.ScrollToRow ( this.grid );
this.CheckDataState( this );
return status;
}
public void OpenTableComboBox()
{
this.tablesCombo2.DroppedDown = true;
this.tablesCombo2.Focus();
}
public void RefreshData()
{
DataGridCell previousPosition = this.grid.CurrentCell;
switch ( this.dataManager.DetailLevel ){
case DetailLevel.TableLevel:
this.RefreshDataBaseData();
break;
case DetailLevel.DataLevel:
this.RefreshTableData();
break;
}
this.grid.CurrentCell = previousPosition;
if( this.grid.CurrentRowIndex > -1 ){
this.grid.Select( this.grid.CurrentRowIndex );
}
}
#endregion
#region Private functions
private void RefreshTableData()
{
if( this.grid.DataSource == null ){
return;
}
this.Cursor = Cursors.WaitCursor;
YariSoft.Utils.YMessages.ChangeCursor ( this.Cursor );
this.grid.DataSource = null;
this.grid.DataSource = this.dataManager.RefreshData( this.tablesCombo2.Text );
this.Cursor = Cursors.Default;
YariSoft.Utils.YMessages.ChangeCursor ( this.Cursor );
}
private void RefreshDataBaseData()
{
this.dataManager.Clear();
this.MoveDown();
this.tablesCombo2.DataSource = this.grid.DataSource;
if( this.tablesCombo2.DataSource != null ){
this.tablesCombo2.DisplayMember = "TABLE_NAME";
} else {
this.tablesCombo2.Items.Clear();
}
this.serverLabel.Text = this.dataManager.ServerCaption;
this.serverLabel.Size = new Size ( this.serverLabel.PreferredWidth, this.serverLabel.PreferredHeight );
this.grid.CaptionText = this.dataManager.ServerCaption;
this.tablesCombo2.Left = this.serverLabel.PreferredWidth;
}
private void grid_Click(object sender, System.EventArgs e)
{
if( this.OnGridClick != null ){
this.OnGridClick( this, sender, e );
}
this.RefreshCaptions();
}
private void grid_Navigate(object sender, System.Windows.Forms.NavigateEventArgs ne)
{
if( ne.Forward ){
this.dataManager.FillTableDataByFK ( this.grid.DataMember );
}
this.RefreshCaptions();
}
private void FillData()
{
DataView view = this.dataManager.FillData( this.tablesCombo2.Text );
this.grid.DataSource = view;
if( view != null ){
DataGridTableStyle style = grid.TableStyles[ view.Table.TableName ];
if( style == null ){
style = new DataGridTableStyle();
style.MappingName = view.Table.TableName;
this.grid.TableStyles.Add( style );
}
if( this.OnNeedSetGridStyle != null ){
this.OnNeedSetGridStyle ( this, style );
}
}
}
private void DownClick(object sender, System.EventArgs e)
{
this.MoveDown();
}
private void UpClick(object sender, System.EventArgs e)
{
this.MoveUp();
}
private void tablesCombo_SelectedIndexChanged(object sender, System.EventArgs e)
{
if( this.dataManager.DetailLevel > YariSoft.DBCommander.DetailLevel.TableLevel ){
this.Cursor = Cursors.WaitCursor;
YariSoft.Utils.YMessages.ChangeCursor ( this.Cursor );
this.FillData();
this.Cursor = Cursors.Default;
YariSoft.Utils.YMessages.ChangeCursor ( this.Cursor );
}
this.grid_Click(sender, e);
}
private void ShowSelectionForm( Modals.SelectionForm.SelectOperationType SelectType )
{
/*
if( this.dataManager.DetailLevel != YariSoft.DBCommander.DetailLevel.DataLevel ){
return;
}
*/
if( this.grid.DataSource == null ){
return;
}
ArrayList selections = null;
if( this.OnNeedSelectionMasks != null ){
this.OnNeedSelectionMasks ( ref selections );
}
Modals.SelectionForm selection = new Modals.SelectionForm();
if( selection.Init ( this.grid,
this.GetActiveDataView( this.grid ),
SelectType,
selections ) ){
selection.ShowDialog();
}
selection.Dispose();
this.grid.Focus();
this.RefreshCaptions();
}
private bool grid_YOnKeyDown( ref Message msg, Keys keyData )
{
if( msg.Msg == 0x100 ){
if( keyData == ( Keys.Add ) ){
this.ShowSelectionForm( Modals.SelectionForm.SelectOperationType.Select );
return true;
}
if( keyData == ( Keys.Subtract ) ){
this.ShowSelectionForm( Modals.SelectionForm.SelectOperationType.Unselect );
return true;
}
if( keyData == ( Keys.Multiply ) ){
this.ShowSelectionForm( Modals.SelectionForm.SelectOperationType.Filter );
return true;
}
if( keyData == ( Keys.Divide ) ){
this.GetActiveDataView( this.grid ).RowFilter = "";
this.RefreshCaptions();
return true;
}
if ( keyData == Keys.Enter ){
if( this.dataManager.DetailLevel < YariSoft.DBCommander.DetailLevel.DataLevel ){
this.MoveDown();
return true;
}
}
if ( keyData == Keys.Delete ){
if( this.grid.IsSelected ( this.grid.CurrentRowIndex ) ){
return true;
}
}
}
return false;
}
private void tablesCombo_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if( e.KeyData == Keys.Enter ){
this.grid.Focus();
}
}
private void grid_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Left ){
this.grid_Click ( sender, null );
}
}
private void grid_CurrentCellChanged(object sender, System.EventArgs e)
{
DataView view = this.GetActiveDataView( this.grid );
if( view != null ){
this.dataManager.UpdateData( view.Table );
this.CheckDataState( this );
}
}
private void CheckDataState( DataPanel Panel )
{
if( Manager.NeedRefresh ){
if( YariSoft.Utils.YMessages.Show( "Current state of data in the grid has expired. Do you want refresh data?",
"Refresh data",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question ) == DialogResult.Yes ) {
Panel.RefreshData();
}
}
}
private DataView GetActiveDataView( DataGrid Grid )
{
if( Grid == null || Grid.DataSource == null ){
return null;
}
return ( DataView )(( CurrencyManager )BindingContext[ Grid.DataSource, Grid.DataMember ]).List;
}
private void RefreshCaptions()
{
int rowCount = 0;
int selCount = 0;
DataView view = this.GetActiveDataView( this.grid );
if( view != null ){
rowCount = this.grid.BindingContext[this.grid.DataSource, this.grid.DataMember].Count;
for( int i = 0; i < rowCount; i++ ){
if( this.grid.IsSelected ( i ) ){
selCount ++;
}
}
}
this.rowsLabel.Text = "Rows: " + rowCount.ToString() + " ";
this.rowsLabel.Text += "Selected: " + selCount.ToString();
}
private void grid_DataSourceChanged(object sender, System.EventArgs e)
{
this.RefreshCaptions();
}
private void ScrollToRow( YariSoft.Windows.Conrols.YDataGrid Grid )
{
if( Grid.DataSource != null &&
Grid.CurrentRowIndex > -1 &&
Grid.BindingContext[Grid.DataSource, Grid.DataMember].Count > 0 &&
Grid.CurrentRowIndex < Grid.BindingContext[Grid.DataSource, Grid.DataMember].Count ){
Grid.ScrollToRow( Grid.CurrentRowIndex );
Grid.Select( Grid.CurrentRowIndex );
}
}
private void grid_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if( e.Button == MouseButtons.Left ){
System.Drawing.Point pt = new Point(e.X, e.Y);
DataGrid.HitTestInfo hti = this.grid.HitTest(pt);
if(hti.Type == DataGrid.HitTestType.ColumnHeader) {
if( this.OnColumnResize != null ){
this.OnColumnResize( this, hti );
}
}
}
}
#endregion
}
}