-
Notifications
You must be signed in to change notification settings - Fork 0
/
FieldTableDate.cs
89 lines (83 loc) · 3.49 KB
/
FieldTableDate.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Theater
{
class FieldTableDate
{
private DateTimePicker _dateTimePicker;
private DataGridView _dataGridView;
private int _indexColumn;
public FieldTableDate(DataGridView _dGridView, int _columnIndex)
{
/* Инициализация объекта ДатаВремя*/
_dateTimePicker = new DateTimePicker();
_dateTimePicker.Format = DateTimePickerFormat.Short;
_dateTimePicker.Visible = false;
_dateTimePicker.Width = 100;
_dGridView.Controls.Add(_dateTimePicker);
_dateTimePicker.ValueChanged += this.dateTimePicker_ValueChanged;
_dGridView.CellBeginEdit += this.dataGridView_CellBeginEdit;
_dGridView.CellEndEdit += this.dataGridView_CellEndEdit;
_dataGridView = _dGridView;
_indexColumn = _columnIndex;
}
/* Начало редактирование ячейки */
private void dataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
try
{
if ((_dataGridView.Focused) && (_dataGridView.CurrentCell.ColumnIndex == _indexColumn))
{
_dateTimePicker.Location = _dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, false).Location;
_dateTimePicker.Visible = true;
if (_dataGridView.CurrentCell.Value != DBNull.Value)
{
_dateTimePicker.Value = (DateTime)_dataGridView.CurrentCell.Value;
}
else
{
_dateTimePicker.Value = DateTime.Today;
}
}
else
{
_dateTimePicker.Visible = false;
}
}
catch (Exception ex)
{
if (MessageBox.Show("Произошла ошибка!" + System.Environment.NewLine + "Показать полное сообщение?", "Ошибка:", MessageBoxButtons.YesNo) == DialogResult.Yes) //Сообщение об ошибке
{
MessageBox.Show(ex.ToString());
}
}
}
/* Завершение редактирвоания ячейки */
private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
try
{
if ((_dataGridView.Focused) && (_dataGridView.CurrentCell.ColumnIndex == _indexColumn))
{
_dataGridView.CurrentCell.Value = _dateTimePicker.Value.Date;
_dateTimePicker.Visible = false;
}
}
catch (Exception ex)
{
if (MessageBox.Show("Произошла ошибка!" + System.Environment.NewLine + "Показать полное сообщение?", "Ошибка:", MessageBoxButtons.YesNo) == DialogResult.Yes) //Сообщение об ошибке
{
MessageBox.Show(ex.ToString());
}
}
}
/* Событие: выбор даты */
private void dateTimePicker_ValueChanged(object sender, EventArgs e)
{
_dataGridView.CurrentCell.Value = _dateTimePicker.Text;
}
}
}