我有一个datagridview,如下所示。在CellEndEdit事件中,它更新MySql数据库中的表 - 但是只有当您单击另一个单元格时,才会触发输入,制表符,向下箭头等。
DataAdapter da = new DataAdapter();
DataGridView dg = new DataGridView();
BindingSource bs = new BindingSource();
DataTable dt = new DataTable();
private void fillDataGrid()
{
da.SelectCommand = new MySqlCommand("SELECT * FROM Table WHERE idBlah = @id;", mySqlCon);
da.Fill(dt);
bs.DataSource = dt;
dg.DataSource = bs;
MySqlCommandBuilder cmb = new MySqlCOmmandBuilder(da);
}
void da_CellEndEdit(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
da.Update((DataTable)bs.DataSource);
}
我在自定义datagridview类中覆盖了以下方法,以尝试跳过被触发的内容。
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter)
{
this.EndEdit();
return true;
}
return base.ProcessDialogKey(keyData);
}
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.EndEdit();
return true;
}
return base.ProcessDataGridViewKey(e);
}
任何建议将不胜感激。我不知道从哪里开始。
EDIT 实际代码:
private BindingSource bindingSourceDependents = new BindingSource();
private MySqlDataAdapter dataAdapterDependents = new MySqlDataAdapter();
private DataTable dt_Dependents = new DataTable();
private void FillDataGridDependents()
{
MySqlCommandBuilder cmb = new MySqlCommandBuilder(dataAdapterDependents);
dataAdapterDependents.SelectCommand = new MySqlCommand("SELECT * FROM Dependents WHERE idGuarentors = " + guarentor.Id + ";", MySqlQueries_Global.MySqlCon());
dt_Dependents = Guarentor.SelectDependents(guarentor.Id);
bindingSourceDependents.DataSource = dt_Dependents;
dataGridViewDependent.DataSource = bindingSourceDependents;
}
private void cellEndEdit(BindingSource bindingSource, DataGridView dg, MySqlDataAdapter da, string columnNameThatNN, int rowIndex, string columnNameForDup, string dupeMessage, Action<string> insertMethod, Action fillMethod)
{
if (dg.Rows[rowIndex].Cells[columnNameThatNN].Value != DBNull.Value)
{
try
{
da.Update((DataTable)bindingSource.DataSource);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
if (dg.Rows[rowIndex].Cells[columnNameForDup].Value != DBNull.Value)
{
string value = dg.Rows[rowIndex].Cells[columnNameForDup].Value.ToString();
bool contains = ((DataTable)bindingSource.DataSource).AsEnumerable()
.Any(row => value.ToUpper() == row.Field<String>(columnNameForDup).ToUpper());
if (!contains)
{
insertMethod(value);
}
else
{
MessageBox.Show(dupeMessage);
fillMethod();
}
}
else { fillMethod(); }
}
}
void dataGridViewDependent_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
cellEndEdit(bindingSourceDependents, dataGridViewDependent, dataAdapterDependents, "idDependents", e.RowIndex, "idDependents", "Critical ERROR contact admin!", dependent.blankmethod, FillDataGridDependents);
}
解决方案:
改变:
da.Update(((DataTable)bindingSource.DataSource));
为:
bindingSource.ResetBindings(true);
da.Update(((DataTable)bindingSource.DataSource));
答案 0 :(得分:1)
改变:
da.Update(((DataTable)bindingSource.DataSource));
为:
bindingSource.ResetBindings(true);
da.Update(((DataTable)bindingSource.DataSource));