public partial class DataGridViewEx:System.Windows.Forms.DataGridView
{
// 紀錄可編輯的欄位名稱
private StringDictionary ColmmEdit;
// 紀錄要使用日期資料的欄位名稱
private StringDictionary ColmmDate;
public DataGridViewEx() : base()
{
this.EditMode = DataGridViewEditMode.EditProgrammatically;
if (!DesignMode)
{
ColmmEdit = new StringDictionary();
ColmmDate = new StringDictionary();
this.Disposed += DataGridViewEx_Disposed;
this.CellClick += DataGridViewEx_CellClick;
this.EditingControlShowing += DataGridViewEx_EditingControlShowing;
}
}
// 刪除資料清單
private void DataGridViewEx_Disposed(object sender, EventArgs e)
{
if (!DesignMode)
{
ColmmEdit.Clear();
ColmmDate.Clear();
}
}
// 設定可編輯的欄位
public void SetColmmEditName(string NameValue)
{
if(!ColmmEdit.ContainsKey(NameValue))
{
ColmmEdit.Add(NameValue, NameValue);
}
}
// 設定要使用DateTimePicker的欄位
public void SetColmmDateName(string NameValue)
{
if (!ColmmDate.ContainsKey(NameValue))
{
ColmmDate.Add(NameValue, NameValue);
}
}
// 檢查欄位是否可以編輯
private void DataGridViewEx_CellClick(object sender, DataGridViewCellEventArgs e)
{
if ((e.RowIndex > -1) || (e.ColumnIndex > -1))
{
if (ColmmEdit.ContainsKey(this.Columns[e.ColumnIndex].Name))
{
this.BeginEdit(true);
}
else
{
this.EndEdit();
}
}
}
// DateTimePicker的日期資料回寫欄位
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
this .CurrentCell.Value = ((DateTimePicker)sender).Value.ToString("yyyy/MM/dd");
}
// 判斷是否適用DateTimePicker的欄位
private void DataGridViewEx_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (this.CurrentCell.ColumnIndex > -1)
{
Control parentCTL = e.Control.Parent;
if(ColmmDate.ContainsKey(this.Columns[this.CurrentCell.ColumnIndex].Name))
{
DateTimePicker dtPicker = new DateTimePicker();
dtPicker.Name = "dateTimePicker1";
dtPicker.Size = this .CurrentCell.Size;
dtPicker.Format = DateTimePickerFormat.Custom;
dtPicker.CustomFormat = "yyyy/MM/dd";
dtPicker.Location = new Point(
e.Control.Location.X - e.Control.Margin.Left < 0 ? 0 : e.Control.Location.X - e.Control.Margin.Left,
e.Control.Location.Y - e.Control.Margin.Top < 0 ? 0 : e.Control.Location.Y - e.Control.Margin.Top);
if (e.Control.Text != "")
{
dtPicker.Value = DateTime.ParseExact(e.Control.Text, dtPicker.CustomFormat, null);
}
else
{
dtPicker.Value = DateTime.Now;
this.CurrentCell.Value = dtPicker.Value.ToString("yyyy/MM/dd");
}
e.Control.Visible = false;
foreach (Control tmpCTL in parentCTL.Controls)
{
if (tmpCTL.Name == dtPicker.Name) parentCTL.Controls.Remove(tmpCTL);
}
parentCTL.Controls.Add(dtPicker);
dtPicker.ValueChanged += new EventHandler(dateTimePicker1_ValueChanged);
}
else
{
foreach (Control tmpCTL in parentCTL.Controls)
{
if (tmpCTL.Name == "dateTimePicker1")
{
parentCTL.Controls.Remove(tmpCTL);
}
}
}
}
}
}
沒有留言:
張貼留言