我正在尝试使用自动生成的AdvancedDatagrid -ADGV(adgv.codeplex.com)。当前的问题类似于以前解决的问题:我以前的stackoverflow.com问题
我有一个表单(frmlev),带有AdvancedDatagrid的表格,其中填充了来自SQL的数据。DataGridView的一列是将DateTimePicker放置的日期字段(DD.MM.Yyyy)。
如果我正确地做到了...问题在于,只有在用户在用户选择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)));
}
您也可以调用Cell的EndEdit()方法。这样的东西:
this.BeginInvoke((Action)(() => levDGV.CurrentCell.EndEdit()));
找到了解决方法...它没有回答我的问题,但对我有用。
this.BindingContext[levDGV.DataSource].EndCurrentEdit(); //does not call CellLeave
currentRowSave();