using HotelPms.Share.Data;
using HotelPms.Share.Util;
using HotelPms.Share.Windows.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace HotelPms.Share.Windows.Tool
{
public partial class MstList : Form
{
/// ****************************** Description *******************************
/// VXe¼Ì
/// @\ñZ^[ÇVXe
/// Tv
/// @êIðæÊ
/// ð
/// @20090126 vic VKì¬
/// ****************************** Declarations ******************************
#region @Declartions@
[Flags]
public enum Style : int
{
None = 0x1,
HideBottom = 0x2,
HideReturnField = 0x4,
HideGridHeader = 0x8,
}
private bool isReturnRow = false;
public bool IsReturnRow
{
set { isReturnRow = value; }
}
private DataGridViewRow m_CurrentRow = null;
public object SelecetdItem
{
get
{
return m_CurrentRow;
}
}
///
/// õíÞ
///
public enum SearchType
{
Custom = 0, //Sql¶ð[U[©çwè·é
DataTable, //.ShowDataTableÉDataTableðwè·é
}
private DataView DtView = new DataView();
// ¤ÊÏ
private int intReturnField_IndexNo; // ReturnFieldÌIndex
private string strSqlSelected; // Sql¶ÌIðiÀÛÉÀs·éSQL¶)
private string strSql = string.Empty;
private string strAddCondition = string.Empty;
private string strReturnField = "R[h";
private string strSearchField = "¼Ì";
private string strReturnValue = string.Empty;
private List colWidth = new List();
private bool strVisibleBottom = true;
private bool strVisibleReturnField = true;
private bool visibleGridHeader = true;
private Style style = Style.None;
#endregion
#region @Property@
///
/// OwnerControl
///
public DBConnectItem DBConnectData { get; set; }
///
/// OwnerControl
///
public Control OwnerControl { get; set; }
///
/// SelectType
///
public SearchType SelectType { get; set; }
///
/// ShowDataTable(\¦·éf[^e[uðwè·éA½¾µAftHgÌêR[hA¼ÌJªKv)
///
public DataTable DtTable { get; set; }
///
/// Sql
///
public string Sql
{
set { strSql = value; }
get { return strSql; }
}
///
/// AddCondition
///
public string AddCondition
{
set { strAddCondition = value; }
get { return strAddCondition; }
}
///
/// ReturnField(Iðµ½Æ«ÉÔ·tB[h¼)
///
public string ReturnField
{
set { strReturnField = value; }
get { return strReturnField; }
}
///
/// SearchField
///
public string SearchField
{
set { strSearchField = value; }
get { return strSearchField; }
}
public Style CurStyle
{
set { style = value; }
get { return style; }
}
///
/// VisibleBottom
///
public List ColWidth
{
set { colWidth = value; }
get { return colWidth; }
}
///
/// ReturnValue(IðãÉæ¾Å«él)
///
public string ReturnValue
{
get { return strReturnValue; }
}
#endregion
#region @Class Event@
///
/// MstList
///
public MstList()
{
InitializeComponent();
}
private void SetLocation()
{
DataGridView grid = OwnerControl as DataGridView;
Form ownerForm = GeneralSub.GetOwnerForm(grid);
//\¦Êu²®
System.Drawing.Point location = new System.Drawing.Point(0, 0);
System.Drawing.Point baseLocation = GeneralSub.GetPopUpLocationEx(ownerForm, grid);
location.X = baseLocation.X;
location.Y = baseLocation.Y;
if (location.X + this.Width > Screen.PrimaryScreen.Bounds.Width)
{
location.X = baseLocation.X - (location.X + this.Width - Screen.PrimaryScreen.Bounds.Width + 2);
}
if (location.Y + this.Height > Screen.PrimaryScreen.Bounds.Height)
{
location.Y = baseLocation.Y - grid.Rows[grid.CurrentCell.RowIndex].Height - this.Height;
}
this.SetBounds(location.X, location.Y, this.Width, this.Height);
}
///
/// MstList_Load
///
private void MstList_Load(object sender, EventArgs e)
{
//\¦CAEg
if (OwnerControl != null)
{
if (OwnerControl is DataGridView)
{
SetLocation();
}
else
{
this.StartPosition = FormStartPosition.Manual;
Point p = this.OwnerControl.PointToScreen(new Point(0, 0));
int x = p.X;
int y = p.Y + OwnerControl.Height;
if (p.Y + OwnerControl.Height + this.Height > Screen.PrimaryScreen.Bounds.Height)
{
y = p.Y - this.Height;
}
if (p.X + this.Width > Screen.PrimaryScreen.Bounds.Width)
{
x = Screen.PrimaryScreen.Bounds.Width - this.Width;
}
this.Location = new Point(x, y);
}
}
//if (msSqlNet == null) { msSqlNet = EnvironmentSetting.DbAccess; }
if ((style & Style.HideBottom) == Style.HideBottom) { strVisibleBottom = false; }
if ((style & Style.HideGridHeader) == Style.HideGridHeader) { visibleGridHeader = false; }
if ((style & Style.HideReturnField) == Style.HideReturnField) { strVisibleReturnField = false; }
//DefaultDataTableÝè
SetDefaultHeader();
//Sql¶ÌIð
GetData();
DtTable.TableName = "SelectTable";
// DataViewÉDataSetÌe[uðA·é
DtView.Table = DtTable;
DtView.RowFilter = "";
// f[^ð\¦
this.DataGridView1.DataSource = DtView;
//ºÌõL[ÆÂ¶é{^ð\¦·é©(úl:True(\¦))
if (this.strVisibleBottom == false)
{
this.panel2.Visible = false; // plPÊÅñ\¦Ö
}
// DataGridView1ÌÝè
//this.DataGridView1.RowHeadersVisible = false; //swb_[ðñ\¦
//this.DataGridView1.AutoResizeColumns( "AllCells", true); // ñð©®²®
//ReturnField(ßèlðÔ·Úñ¼)ÌIndexNoðæ¾·éB
intReturnField_IndexNo = -1;
if (strReturnField == string.Empty)
{
intReturnField_IndexNo = 0;
}
else
{
for (int i = 0; i < this.DataGridView1.Columns.Count; i++)
{
if (this.DataGridView1.Columns[i].HeaderText.ToString() == strReturnField.ToString())
{
intReturnField_IndexNo = i;
}
}
}
if (intReturnField_IndexNo == -1)
{
intReturnField_IndexNo = 0;
strReturnField = string.Empty;
strSearchField = string.Empty;
}
//ReturnField(ßèlðÔ·Úñ¼)ðæ¾·éB
if (this.strReturnField.Length == 0)
{
this.strReturnField = this.DtView.Table.Columns[intReturnField_IndexNo].ColumnName.ToString();
}
//SearchField(õL[ÉÖA³¹éÚñ¼)ðæ¾·éB
if (this.strSearchField.Length == 0)
{
this.strSearchField = this.DtView.Table.Columns[intReturnField_IndexNo + 1].ColumnName.ToString();
}
//VisibleReturnField = falseÌêAReturnFieldðñ\¦Æ·éB
if ((strVisibleReturnField == false) && (intReturnField_IndexNo > -1))
{
this.DataGridView1.Columns[intReturnField_IndexNo].Visible = false;
}
if (DtTable.Rows.Count == 0)
{
MessageBox.Show("Yf[^ª èܹñB");
this.Close();
return;
}
this.DataGridView1.ColumnHeadersVisible = visibleGridHeader;
//G[
if (!ValidateInput())
{
Debug.Assert(false, "JpError", "SelectªsKØAsearchFieldªlA¶ñÈO");
}
DataGridView1.EnableHeadersVisualStyles = false;
DataGridView1.ColumnHeadersHeight = 25;
foreach (DataGridViewColumn col in DataGridView1.Columns)
{
col.HeaderCell.Style.BackColor = System.Drawing.Color.FromArgb(50, 50, 100);
col.HeaderCell.Style.ForeColor = Color.White;
}
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;//COMMENT:20080918 Ñ No1060
DataGridView1.AllowUserToResizeColumns = true;
if (colWidth == null || colWidth.Count == 0)
{
DataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
return;
}
for (int i = 0; i < colWidth.Count; i++)
{
DataGridView1.Columns[i].Width = colWidth[i];
}
}
#region EscL[@妎
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == Keys.Escape)
{
//eXgpbZ[W
//MessageBox.Show("妎{^NbN");
//ßèlƵÄÝè(Emptyl)
strReturnValue = string.Empty;
//æÊð¶é
this.Close();
}
return base.ProcessCmdKey(ref msg, keyData);
}
#endregion
#endregion
#region @Control Event@
///
/// 妎{^@NbNCxg
///
private void btnClose_Click(object sender, EventArgs e)
{
//eXgpbZ[W
//MessageBox.Show("妎{^NbN");
//ßèlƵÄÝè(Emptyl)
strReturnValue = string.Empty;
//æÊð¶é
this.Close();
}
///
/// DataGridView1_CellMouseDoubleClick
///
private void DataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (DataGridView1.RowCount > 0)
{
//eXgpbZ[W
//int iCol = intReturnField_IndexNo; //ñ(ReturnFieldÌñÖÅè)
//int iRow = this.DataGridView1.CurrentCell.RowIndex; //s(Iðs)
//MessageBox.Show("ñ" + iCol.ToString() + ":s" + iRow.ToString() + "Ì" + CellValue.ToString.ToString() + "ªIð³êĢܷ");
// ANeBuÈZÌlðßèlƵÄÝè
if (!isReturnRow)
{
strReturnValue = this.DataGridView1[this.strReturnField.ToString(), this.DataGridView1.CurrentCell.RowIndex].Value.ToString(); //Celll
}
else
{
m_CurrentRow = this.DataGridView1.CurrentRow;
}
//æÊð¶é
this.Close();
}
}
///
/// DataGridView1_KeyDown
///
private void DataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (DataGridView1.RowCount > 0)
{
if (!isReturnRow)
{
strReturnValue = this.DataGridView1[this.strReturnField.ToString(), this.DataGridView1.CurrentCell.RowIndex].Value.ToString(); //Celll
}
else
{
m_CurrentRow = this.DataGridView1.CurrentRow;
}
//æÊð¶é
this.Close();
}
}
else if (e.KeyCode == Keys.Tab)
{
e.Handled = true;
if (e.Shift == true)
{
this.btnClose.Focus();
}
else
{
this.txtSearch.Focus();
}
}
}
///
/// txtSearch_KeyDown
///
private void txtSearch_KeyDown(object sender, KeyEventArgs e)
{
//string strFilterSql;
//EnterL[üÍÌê
if (e.KeyCode == Keys.Enter)
{
//DataGridView1ÖtH[JXÚ®
this.DataGridView1.Focus();
}
}
///
/// txtSearch_KeyDown
///
private void txtSearch_TextChanged(object sender, EventArgs e)
{
string strFilterSql;
// tB^[ðð»Ê
if ((this.txtSearch.Text.ToString() == string.Empty) || (this.strSearchField.ToString() == string.Empty))
{
//tB^ð³µ(SÄð\¦)
strFilterSql = string.Empty;
}
else
{
//tB^ðwè
strFilterSql = this.strSearchField.ToString() + " LIKE '%" + this.txtSearch.Text.ToString() + "%'";
}
//eXgpbZ[W
//MessageBox.Show("tB^[F" + strFilterSql.ToString());
// tB^[ððwèµÄDataViewÉi[
DtView.RowFilter = strFilterSql.ToString();
// f[^ð\¦
this.DataGridView1.DataSource = DtView;
}
private void txtSearch_Enter(object sender, EventArgs e)
{
txtSearch.BackColor = Color.LightSteelBlue;
}
private void txtSearch_Leave(object sender, EventArgs e)
{
txtSearch.BackColor = Color.White;
}
#endregion
#region @Private Function@
///
/// DefaultÌÝè
///
private void SetDefaultHeader()
{
if (SelectType != SearchType.DataTable)
{
DtTable.Columns.AddRange(new DataColumn[] { new DataColumn("R[h"), new DataColumn("¼Ì") });
}
}
///
/// ValidateInput
///
private bool ValidateInput()
{
if (isReturnRow) { return true; }
if (DtTable.Rows.Count == 0)
return false;
if (
DtTable.Columns[this.strSearchField].DataType != typeof(string)
// && DtSet.Tables[0].Columns[this.strSearchField].DataType != typeof(int)
)
return false;
return true;
}
///
/// SelectSql
///
private void GetData()
{
strSqlSelected = string.Empty;
//Sql¶ÌIð
if (SelectType.Equals(SearchType.Custom))
{ //JX^Ýè
strSqlSelected = this.strSql.ToString() + " " + this.strAddCondition.ToString();
// f[^ðæ¾
using (MsSqlNet msSqlNet = new MsSqlNet(DBConnectData.HostName, DBConnectData.UserID, DBConnectData.Password, DBConnectData.DBName))
{
DtTable = msSqlNet.GetDataTable(this.strSqlSelected.ToString());
}
}
}
#region ------------ Åèf[^Ìe[uf[^ -------------------
///
/// EnumÉæèf[^e[uðìé
///
public DataTable GetTypeData(Type enumType)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("R[h"), new DataColumn("¼Ì") });
DataRow row = null;
foreach (object item in Enum.GetValues(enumType))
{
row = dt.NewRow();
dt.Rows.Add(row);
row["R[h"] = ((int)item).ToString();
row["¼Ì"] = item.ToString();
}
return dt;
}
#endregion
#endregion
#region @Public Function@
public static string GetTypeName(Type enumType,int code)
{
try
{
return CConvert.ToString(Enum.GetName(enumType, code));
}
catch
{
return string.Empty;
}
}
public static string ExecuteSql(Control owner, DBConnectItem dbItem, string sql)
{
return Execute(SearchType.Custom, owner, dbItem, Style.None, Size.Empty, string.Empty, string.Empty, string.Empty, null, sql, null);
}
public static string ExecuteTable(Control owner, DataTable data)
{
return Execute(SearchType.DataTable, owner, null, Style.None, Size.Empty, string.Empty, string.Empty, string.Empty, null, string.Empty, data);
}
///
/// ê\¦
///
///
public static string Execute(SearchType type, Control owner, DBConnectItem dbItem)
{
return Execute(type, owner, dbItem, Style.None, Size.Empty, string.Empty, string.Empty, string.Empty, null, string.Empty, null);
}
public static string Execute(SearchType type, Control owner, DBConnectItem dbItem, Style style)
{
return Execute(type, owner, dbItem, style, Size.Empty, string.Empty, string.Empty, string.Empty, null, string.Empty, null);
}
public static string Execute(SearchType type, Control owner, DBConnectItem dbItem, Style style, Size size)
{
return Execute(type, owner, dbItem, style, size, string.Empty, string.Empty, string.Empty, null, string.Empty, null);
}
public static string Execute(SearchType type, Control owner, DBConnectItem dbItem, Style style, Size size, string addCondition)
{
return Execute(type, owner, dbItem, style, size, addCondition, string.Empty, string.Empty, null, string.Empty, null);
}
public static string Execute(SearchType type, Control owner, DBConnectItem dbItem, Style style, Size size, string addCondition,
string returnField, string searchField, List colWidth)
{
return Execute(type, owner, dbItem, style, size, addCondition, returnField, searchField, colWidth, string.Empty, null);
}
public static string Execute(SearchType type, Control owner, DBConnectItem dbItem, Style style, Size size, string addCondition,
string returnField, string searchField, List colWidth, string sql, DataTable data)
{
string result = string.Empty;
MstList mstList = new MstList();
mstList.SelectType = type;
mstList.OwnerControl = owner;
mstList.DBConnectData = dbItem;
mstList.CurStyle = style;
if (!size.Equals(Size.Empty)) { mstList.Size = size; }
if (returnField.Length > 0) { mstList.ReturnField = returnField; }
if (searchField.Length > 0) { mstList.SearchField = searchField; }
if (colWidth != null) { mstList.ColWidth = colWidth; }
mstList.AddCondition = addCondition;
mstList.Sql = sql;
mstList.DtTable = data;
mstList.ShowDialog();
if (mstList.ReturnValue.Length > 0)
{
result = mstList.ReturnValue;
}
mstList.Dispose();
return result;
}
#endregion
}
}