我正在尝试使用自动生成的advancedDataGrid - ADGV(adgv.codeplex.com)。 目前的问题类似于之前解决的问题:my previous stackoverflow.com question
我有一个表单(frmLev),其中advancedDataGrid填充了SQL中的数据。 DataGridView的一列是日期字段(DD.MM.YYYY),其中放置了DateTimePicker。
如果我做对了......问题在于,只有当用户在DatePicker中选择日期后手动完成单元格编辑时,整行才会保存到SQL。单元格看起来像编辑完成,但必须通过单击,按Enter或选项卡再次确认。其他文本单元格选择了文本或闪烁光标。
我想在delDatePick_textChange()结束时将行保存到SQL,但此时调用currentRowSave()将无效。用户必须先执行单击。仅当levDGV_CellLeave()获取事件时才会保存数据。
问题是:如何以编程方式触发CellLeave事件?
MetroFramework.Controls.MetroDateTime delDatePick = new MetroFramework.Controls.MetroDateTime();
Rectangle _Rectangle;
private void frmLev_Load(object sender, EventArgs e)
{
//Add DateTimePicker to Date
levDGV.Controls.Add(delDatePick);
delDatePick.Visible = false;
delDatePick.Format = DateTimePickerFormat.Short;
delDatePick.TextChanged += new EventHandler(delDatePick_textChange);
delDatePickValueAutomatic = false;
}
private void levDGV_CellClick(object sender, DataGridViewCellEventArgs e)
{
switch (levDGV.Columns[e.ColumnIndex].HeaderText)
{
case "Datum":
_Rectangle = levDGV.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
delDatePick.Size = new Size(_Rectangle.Width, _Rectangle.Height);
delDatePick.Location = new Point(_Rectangle.X, _Rectangle.Y);
delDatePick.Visible = true;
if (levDGV.CurrentCell.Value != DBNull.Value)
{
delDatePick.Value = (DateTime)levDGV.CurrentCell.Value;
delDatePickValueAutomatic = true;
}
break;
}
}
private void delDatePick_textChange(object sender, EventArgs e)
{
if (delDatePickValueAutomatic != false) // event fired when cell gets focused, so skipping
{
String pickedDate = delDatePick.Text.ToString();
delDatePick.Visible = false;
this.BeginInvoke((MethodInvoker)delegate ()
{levDGV.CurrentCell.Value = pickedDate;});
levDGV.Refresh();
delDatePick.Visible = false;
//I want to update SQL row here, but calling currentRowSave() will not work at this moment because row is in edit mode...
}
}
private void levDGV_CellLeave(object sender, DataGridViewCellEventArgs e)
{
//skipping first entrance;
if (e.ColumnIndex != 0 && e.RowIndex != 0)
{
delDatePick.Visible = false;
currentRowSave();
}
}
private void currentRowSave()
{
int rowIdx = levDGV.CurrentCell.RowIndex;
DataRowView drv = (DataRowView)levDGV.Rows[rowIdx].DataBoundItem;
DataRow dr = drv.Row;
BeginInvoke((Action)(() => SaveRowChanges(dr)));
}
答案 0 :(得分:1)
您也可以调用单元格的EndEdit()方法。像这样:
this.BeginInvoke((Action)(() => levDGV.CurrentCell.EndEdit()));
答案 1 :(得分:0)
找到一个解决方法......它没有回答我的问题,但对我有用。
this.BindingContext[levDGV.DataSource].EndCurrentEdit(); //does not call CellLeave
currentRowSave();