using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Drawing;
|
using System.Data;
|
using System.Text;
|
using System.Windows.Forms;
|
|
namespace HotelPms.Share.Windows.Component
|
{
|
[ToolboxBitmap(typeof(DataGridView))]
|
public partial class DataGridViewEx : DataGridView
|
{
|
#region Privateè
|
|
private const int WM_KEYDOWN = 0x100;
|
private const int WM_KEYUP = 0x101;
|
private const int WM_CHAR = 0x102;
|
private int orgRowIndex = 0;
|
|
#endregion
|
|
/// <summary>
|
/// RXgN^
|
/// </summary>
|
public DataGridViewEx()
|
{
|
InitializeComponent();
|
|
SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
|
UpdateStyles();
|
|
txtKanji.Visible = false;
|
this.Controls.Add(txtKanji);
|
txtKanji.AutoSize = false;
|
txtKanji.BorderStyle = BorderStyle.None;
|
txtKanji.Leave += new EventHandler(txtKanji_Leave);
|
|
this.KeyDown += new KeyEventHandler(this.CancelEndKeyEvent);
|
this.KeyUp += new KeyEventHandler(this.InsertRowEvent);
|
this.KeyUp += new KeyEventHandler(this.DeleteRowEvent);
|
//this.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.RepaintGrid);
|
this.CellPainting += new System.Windows.Forms.DataGridViewCellPaintingEventHandler(this.OnCellPainting);
|
this.CellEnter += this.DoCellEnter;
|
this.CellLeave += this.DoCellLeave;
|
this.SelectionChanged += new EventHandler(DataGridViewEx_SelectionChanged);
|
this.RowLeave += new DataGridViewCellEventHandler(DataGridViewEx_RowLeave);
|
}
|
|
private void DataGridViewEx_RowLeave(object sender, DataGridViewCellEventArgs e)
|
{
|
System.Diagnostics.Debug.WriteLine("DataGridViewEx_RowLeave");
|
orgRowIndex = e.RowIndex;
|
}
|
|
/// <summary>
|
/// SelectionMode = FullRowSelectÌÉAsÏX̵©±ÌCxgðÊçÈ¢
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void DataGridViewEx_SelectionChanged(object sender, EventArgs e)
|
{
|
if(this.CurrentRow == null) { return; }
|
if (orgRowIndex != this.CurrentRow.Index)
|
{
|
RaiseRowChanged();
|
orgRowIndex = this.CurrentRow.Index;
|
}
|
}
|
|
void txtKanji_Leave(object sender, EventArgs e)
|
{
|
SaveKanji();
|
}
|
|
#region PrivateÏ
|
/// <summary>
|
/// ZIðF
|
/// </summary>
|
private Color m_CellSelectionBackColor = Color.Empty;
|
|
/// <summary>
|
/// sIð[hÅ^[L[ÉæéZÚ®®ìÌL³
|
/// </summary>
|
private bool m_EnableReturnOnFullRowSelect = true;
|
|
/// <summary>
|
/// Ì^[L[ÌÚ®ð³øÉ·é
|
/// </summary>
|
private bool m_StopNext = false;
|
/// <summary>
|
///
|
/// </summary>
|
private bool allowAddDelRowByKey = true;
|
|
private int colIndexKanjiToKana = -1; //¿ËJiÏ·ÌñIndex
|
|
#endregion
|
|
#region vpeB
|
|
private bool m_RowStyleInEditEnabled = false;
|
|
[Description("ÒWóÔA¯¶sÌÒWóÔÈOÌZÌwiFÍsX^C©ç©áµÍSÌX^C©"), Category("®ì"), DefaultValue(typeof(bool))]
|
public bool RowStyleInEditEnabled
|
{
|
set
|
{
|
this.m_RowStyleInEditEnabled = value;
|
}
|
get
|
{
|
return this.m_RowStyleInEditEnabled;
|
}
|
}
|
|
|
[Description("¿ËJiÏ·ÌñIndex"), Category("®ì"), DefaultValue(-1)]
|
public int ColIndexKanjiToKana
|
{
|
get { return colIndexKanjiToKana; }
|
set { colIndexKanjiToKana = value; }
|
}
|
|
/// <summary>
|
/// ZIðF
|
/// </summary>
|
[Description("ZIðÌFðæ¾Ü½ÍÝèµÜ·B"), Category("\¦"), DefaultValue(typeof(Color))]
|
public Color CellSelectionBackColor
|
{
|
set
|
{
|
this.m_CellSelectionBackColor = value;
|
}
|
get
|
{
|
return this.m_CellSelectionBackColor;
|
}
|
}
|
|
/// <summary>
|
/// sIð[hÅ^[L[ÉæéZÚ®®ìÌL³
|
/// </summary>
|
[Description("sIð[hÅ^[L[ÉæéZÚ®®ì·é^µÈ¢ðæ¾Ü½ÍÝèµÜ·B"), Category("®ì"), DefaultValue(typeof(bool))]
|
public bool EnableReturnOnFullRowSelect
|
{
|
set
|
{
|
this.m_EnableReturnOnFullRowSelect = value;
|
}
|
get
|
{
|
return this.m_EnableReturnOnFullRowSelect;
|
}
|
}
|
|
/// <summary>
|
/// s}üAíð·é
|
/// </summary>
|
public bool AllowAddDelRowByKey
|
{
|
set
|
{
|
this.allowAddDelRowByKey = value;
|
}
|
get
|
{
|
return this.allowAddDelRowByKey;
|
}
|
}
|
|
#endregion
|
|
public event KeyEventHandler OnEnterEventHandler;
|
public event KeyEventHandler EditKeyDown;
|
public event DataGridViewCellEventHandler RowChanged;
|
|
private bool RaiseRowChanged()
|
{
|
if(this.CurrentCell == null) { return true;}
|
if (RowChanged != null)
|
{
|
RowChanged(this, new DataGridViewCellEventArgs(this.CurrentCell.ColumnIndex, orgRowIndex));
|
}
|
return false;
|
}
|
private bool RaiseEnterEvent()
|
{
|
if (OnEnterEventHandler != null)
|
{
|
KeyEventArgs e = new KeyEventArgs(Keys.Enter);
|
OnEnterEventHandler(this, e);
|
if (e.Handled) { return true; }
|
}
|
return false;
|
}
|
|
private bool RaiseEditKeyDownEvent(Keys keyData)
|
{
|
if (EditKeyDown != null)
|
{
|
KeyEventArgs e = new KeyEventArgs(keyData);
|
EditKeyDown(this, e);
|
if (e.Handled) { return true; }
|
}
|
return false;
|
}
|
|
/// <summary>
|
/// Ì^[L[ÌÚ®ð³øÉ·é
|
/// </summary>
|
public void StopNext()
|
{
|
this.m_StopNext = true;
|
}
|
|
// s}ü
|
protected virtual void InsertRowEvent(object sender, KeyEventArgs e)
|
{
|
if (e.Shift && e.KeyCode == Keys.Insert)
|
{
|
if (this.allowAddDelRowByKey)
|
{
|
this.Rows.Add(new DataGridViewRow());
|
}
|
}
|
}
|
|
// sí
|
protected virtual void DeleteRowEvent(object sender, KeyEventArgs e)
|
{
|
if (e.Shift && e.KeyCode == Keys.Delete)
|
{
|
if (this.allowAddDelRowByKey && this.RowCount > 0)
|
{
|
if (this.CurrentCell.RowIndex == 0)
|
{
|
this[0, this.CurrentCell.RowIndex].Selected = true;
|
this.Rows.RemoveAt(this.CurrentCell.RowIndex);
|
}
|
}
|
}
|
}
|
|
// [End] L[ºÌLZ
|
protected virtual void CancelEndKeyEvent(object sender, KeyEventArgs e)
|
{
|
if (e.KeyCode == Keys.End)
|
{
|
e.SuppressKeyPress = true;
|
}
|
}
|
|
/// <summary>
|
/// ZÌ«EÉüð`æ·é
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void OnCellPainting(object sender, DataGridViewCellPaintingEventArgs e)
|
{
|
DataGridView dataGridView = (DataGridView)sender;
|
Pen GrayPen = Pens.LightGray;
|
|
//OCFÅg`æ
|
e.Graphics.DrawRectangle(GrayPen, e.CellBounds);
|
}
|
|
private DataGridViewSelectionMode orgSelectionMode = DataGridViewSelectionMode.FullRowSelect;
|
protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e)
|
{
|
base.OnCellBeginEdit(e);
|
if (!e.Cancel)
|
{
|
orgSelectionMode = this.SelectionMode;
|
foreach (DataGridViewCell cell in this.CurrentRow.Cells)
|
{
|
if (cell.Equals(this.CurrentCell))
|
{
|
this.CurrentCell.Style.BackColor = this.CurrentCell.Style.SelectionBackColor;
|
}
|
else
|
{
|
cell.Style.BackColor = m_RowStyleInEditEnabled ? this.CurrentRow.DefaultCellStyle.SelectionBackColor : this.DefaultCellStyle.SelectionBackColor;
|
}
|
}
|
this.SelectionMode = DataGridViewSelectionMode.CellSelect;
|
}
|
}
|
|
protected override void OnCellEndEdit(DataGridViewCellEventArgs e)
|
{
|
base.OnCellEndEdit(e);
|
foreach (DataGridViewCell cell in this.CurrentRow.Cells)
|
{
|
cell.Style.BackColor = m_RowStyleInEditEnabled ? this.CurrentRow.DefaultCellStyle.BackColor : this.DefaultCellStyle.BackColor;
|
}
|
this.SelectionMode = orgSelectionMode;
|
if (this.SelectionMode == DataGridViewSelectionMode.FullRowSelect && this.CurrentRow.Selected == false)
|
{
|
this.CurrentRow.Selected = true;
|
}
|
}
|
|
protected override void OnCellEnter(DataGridViewCellEventArgs e)
|
{
|
if (this.SelectionMode == DataGridViewSelectionMode.FullRowSelect && this.CurrentRow.Selected == false)
|
{
|
this.CurrentRow.Selected = true;
|
}
|
base.OnCellEnter(e);
|
}
|
|
protected override void OnEditingControlShowing(DataGridViewEditingControlShowingEventArgs e)
|
{
|
try
|
{
|
if (e.Control is DataGridViewTextBoxEditingControl)
|
{
|
DataGridViewTextBoxEditingControl control = (DataGridViewTextBoxEditingControl)e.Control;
|
control.Enter -= new EventHandler(control_Enter);
|
control.Enter += new EventHandler(control_Enter);
|
}
|
}
|
catch { }
|
base.OnEditingControlShowing(e);
|
}
|
|
private void control_Enter(object sender, EventArgs e)
|
{
|
(sender as TextBox).BackColor = this.CurrentCell.Style.SelectionBackColor;
|
}
|
|
protected override bool DoubleBuffered
|
{
|
set
|
{
|
base.DoubleBuffered = value;
|
}
|
}
|
|
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
|
{
|
//¡sÒWÌÎ
|
if ((keyData & Keys.Control) == Keys.Control && (keyData & Keys.Return) == Keys.Return)
|
{
|
if (EditingControl is DataGridViewTextBoxEditingControl)
|
{
|
DataGridViewTextBoxEditingControl editor = EditingControl as DataGridViewTextBoxEditingControl;
|
if (editor.Visible && editor.Multiline)
|
{
|
editor.Text += Environment.NewLine;
|
editor.SelectionStart = editor.Text.Length;
|
return true;
|
}
|
}
|
}
|
|
if (RaiseEditKeyDownEvent(keyData)) { return true; }
|
|
if (colIndexKanjiToKana > -1)
|
{
|
if (keyData != Keys.Enter && CurrentCell.ColumnIndex == colIndexKanjiToKana)
|
{
|
ShowKanjiCtrl();
|
}
|
else
|
{
|
txtKanji.Visible = false;
|
}
|
|
}
|
return base.ProcessCmdKey(ref msg, keyData);
|
}
|
|
/// <summary>
|
/// ÒWóÔÌAL[ͱ±ÉÊé
|
/// </summary>
|
/// <param name="keyData"></param>
|
/// <returns></returns>
|
protected override bool ProcessDialogKey(Keys keyData)
|
{
|
if (this.m_EnableReturnOnFullRowSelect == true)
|
{
|
//EnterL[ª³ê½ÍATabL[ª³ê½æ¤É·é
|
if ((keyData & Keys.KeyCode) == Keys.Enter)
|
{
|
if (RaiseEnterEvent()) { return true; }
|
|
if (this.m_StopNext == false)
|
{
|
bool flg = this.StandardTab;
|
this.StandardTab = false;
|
bool ret = this.ProcessTabKey(keyData);
|
this.StandardTab = flg;
|
return ret;
|
}
|
else
|
{
|
//³Éß·
|
this.m_StopNext = false;
|
return true;
|
}
|
}
|
}
|
else
|
{
|
if ((keyData & Keys.KeyCode) == Keys.Enter)
|
{
|
return true;
|
}
|
}
|
|
return base.ProcessDialogKey(keyData);
|
}
|
|
/// <summary>
|
/// ÒWóÔÅÍÈ¢ACellãÌL[
|
/// </summary>
|
/// <param name="e"></param>
|
/// <returns></returns>
|
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
|
{
|
if (this.m_EnableReturnOnFullRowSelect == true)
|
{
|
//EnterL[ª³ê½ÍATabL[ª³ê½æ¤É·é
|
if (e.KeyCode == Keys.Enter)
|
{
|
if (RaiseEnterEvent()) { return true; }
|
|
if (this.m_StopNext == false)
|
{
|
bool flg = this.StandardTab;
|
this.StandardTab = false;
|
bool ret = this.ProcessTabKey(e.KeyCode);
|
this.StandardTab = flg;
|
return ret;
|
}
|
else
|
{
|
//³Éß·
|
this.m_StopNext = false;
|
return true;
|
}
|
}
|
}
|
else
|
{
|
if (e.KeyCode == Keys.Enter) { return true; }
|
}
|
|
// [End] L[ºÌLZ
|
if (e.KeyCode == Keys.End)
|
{
|
e.Handled = true;
|
return true;
|
}
|
|
return base.ProcessDataGridViewKey(e);
|
}
|
|
private string ToCString(object value)
|
{
|
try
|
{
|
return value.ToString();
|
}
|
catch
|
{
|
return string.Empty;
|
}
|
}
|
|
private void ShowKanjiCtrl()
|
{
|
if (txtKanji.Visible) { return; }
|
CurrentCell.ReadOnly = true;
|
txtKanji.Font = CurrentCell.Style.Font;
|
|
txtKanji.Location = new Point(RowHeadersVisible ? RowHeadersWidth : 0 + CurrentCell.Size.Width * CurrentCell.ColumnIndex + 2,
|
ColumnHeadersHeight + CurrentCell.Size.Height * CurrentCell.RowIndex);
|
|
|
//txtKanji.Location = new Point(RowHeadersVisible ? RowHeadersWidth : 0 + CurrentCell.Size.Width * CurrentCell.ColumnIndex + (CurrentCell.Size.Width - txtKanji.Width)/2,
|
// ColumnHeadersHeight + CurrentCell.Size.Height * CurrentCell.RowIndex + (CurrentCell.Size.Height - txtKanji.Height)/2);
|
txtKanji.Size = new Size(CurrentCell.Size.Width, CurrentCell.Size.Height);
|
txtKanji.Text = ToCString(CurrentRow.Cells[colIndexKanjiToKana].Value);
|
txtKana.Text = ToCString(CurrentRow.Cells[colIndexKanjiToKana + 1].Value);
|
txtKanji.Visible = true;
|
txtKanji.Focus();
|
}
|
|
private void SaveKanji()
|
{
|
txtKanji.Visible = false;
|
CurrentRow.Cells[colIndexKanjiToKana].Value = txtKanji.Text;
|
CurrentRow.Cells[colIndexKanjiToKana + 1].Value = txtKana.Text;
|
}
|
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void DoCellEnter(object sender, DataGridViewCellEventArgs e)
|
{
|
//wb_[ÈOÌZ
|
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
|
{
|
DataGridView dgv = (DataGridView)sender;
|
|
//ZX^CðÏX·é
|
dgv[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = this.m_CellSelectionBackColor;
|
}
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
private void DoCellLeave(object sender, DataGridViewCellEventArgs e)
|
{
|
//wb_[ÈOÌZ
|
if (e.ColumnIndex >= 0 && e.RowIndex >= 0)
|
{
|
DataGridView dgv = (DataGridView)sender;
|
|
//ZX^Cð³Éß·
|
//dgv[e.ColumnIndex, e.RowIndex].Style.SelectionBackColor = Color.Empty;
|
}
|
}
|
}
|
}
|