winform,DataGridView单元格点击选择日期,日期控件

/// <summary>
/// 新增,工作日历配置
/// </summary>
public partial class FrmCalendarProductivityAdd : BaseForm.FrmBaseForm
{
public static string strUserId, strUserName, personId, personName;
public SqlConnection myConn;
/// <summary>
/// 时间控件
/// </summary>
private DateTimePicker _dtptime = new DateTimePicker();
BasCommand.BasCommand bs = new BasCommand.BasCommand();
public FrmCalendarProductivityAdd(FrmWorkingCalendar _parentFrm)
{
InitializeComponent();
}
private void FrmCalendarProductivityAdd_Load(object sender, EventArgs e)
{
this.hlDataGridView1.AutoGenerateColumns = false;
this.hlDataGridView1.Scroll += (s, e2) =>
{
//滚动条滚动时,隐藏时间控件
_dtptime.Visible = false;
};
//编辑表格中的,检验日期,弹出时间控件
this.Controls.Add(_dtptime);
_dtptime.Visible = false;
_dtptime.Font = new Font("宋体", 11F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(134)));
_dtptime.Format = DateTimePickerFormat.Custom;
_dtptime.CustomFormat = "yyyy-MM-dd";
//更新表格日期值,选择的日期复制给单元格
_dtptime.CloseUp += (o, e45) =>
{
var dtp = o as DateTimePicker;
DataGridViewCell currentCell3 = dtp.Tag as DataGridViewCell;
if (currentCell3 != null)
{
currentCell3.Value = dtp.Value;
currentCell3.DataGridView.EndEdit();
dtp.Visible = false;
}
};
this.hlDataGridView1.CellClick += HlDataGridView1_CellClick;
}
private void HlDataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
var dgv = sender as HLDataGridView;
if (e.RowIndex < 0 || e.ColumnIndex < 0)
{
return;
}
//如果点击的是日期列,则显示日期控件
if (dgv.Columns[e.ColumnIndex].Name == "date")
{
SelectedDate(dgv.CurrentCell);
}
}
/// <summary>
/// 点击时间单元格时,替换为日期控件
/// </summary>
/// <param name="currentCell"></param>
private void SelectedDate(DataGridViewCell currentCell)
{
_dtptime.Tag = null;
var dgv = currentCell.DataGridView;
//获取单元格位置
Rectangle _rect = this.RectangleToClient(dgv.RectangleToScreen(dgv.GetCellDisplayRectangle(currentCell.ColumnIndex, currentCell.RowIndex, true)));
_dtptime.LostFocus += (o, e) =>
{
_dtptime.Visible = false;
};
_dtptime.Location = _rect.Location;
_dtptime.Size = _rect.Size;
_dtptime.BringToFront();
_dtptime.Value = bs.GetDynamicToValue<DateTime>(currentCell.OwningRow.Cells["date"].Value, DateTime.Now.Date);
_dtptime.Visible = true;
_dtptime.BringToFront();
_dtptime.Focus();
_dtptime.Tag = currentCell;
}
1957






