DataGridView row is still dirty after committing changes
DataGridView.IsCurrentRowDirty
remains true
after I commit changes to the database. I want to set it to fa开发者_运维技巧lse
so it doesn't trigger RowValidating
when it loses focus.
I have a DataGridView
bound to a BindingList<T>
. I handle the CellEndEdit
event and save changes to the database. After saving those changes I would like DataGridView.IsCurrentRowDirty
to be set to true
, since all cells in the row are up-to-date; however, it's set to false
.
This causes problems for me because when the row does lose focus it will trigger RowValidating
, which I handle and validate all three cells in. So even though all the cells are valid and none are dirty it will still validate them all. That's a waste.
Here's an example of what I have:
void dataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
// Ignore cell if it's not dirty
if (dataGridView.isCurrentCellDirty)
return;
// Validate current cell.
}
void dataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
// Ignore Row if it's not dirty
if (!dataGridView.IsCurrentRowDirty)
return;
// Validate all cells in the current row.
}
void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
// Validate all cells in the current row and return if any are invalid.
// If they are valid, save changes to the database
// This is when I would expect dataGridView.IsCurrentRowDirty to be false.
// When this row loses focus it will trigger RowValidating and validate all
// cells in this row, which we already did above.
}
I've read posts that said I could call the form's Validate()
method, but that will cause RowValidating
to fire, which is what I'm trying to avoid.
Any idea how I can set DataGridView.IsCurrentRowDirty
to true
? Or maybe a way to prevent RowValidating
from unnecessarily validating all the cells?
Have you tried calling DataGridView1.EndEdit() after saving the data to the database.
I had the same issue with Validating
firing twice. Once before the an edit is comitted (as expected), but then again on what I think was the DataGridView
focus changing.
Didn't have any time to investigate. Quick fix is this.ActiveControl = null;
I'm not sure if this has any unintended consequences yet, but it fixes the validation issue by programmatically unfocussing the control.
private void cntrl_MethodParameters_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
//Init
var dgv = (DataGridView)sender;
int row = e.RowIndex;
int col = e.ColumnIndex;
//Validate Edit
if ((row >= 0) && (col == cntrl_MethodParameters.Columns.IndexOf(cntrl_MethodParameters.Columns[MethodBuilderView.m_paramValueCol])))
{
string xPropertyName = (string)cntrl_MethodParameters[MethodBuilderView.m_paramNameCol, row].EditedFormattedValue;
string xPropertyValue = (string)cntrl_MethodParameters[MethodBuilderView.m_paramValueCol, row].EditedFormattedValue;
bool Validated = FactoryProperties.Items[xPropertyName].SetState(xPropertyValue);
//Cancel Invalid Input
if (!Validated)
{
dgv.CancelEdit();
}
}
this.ActiveControl = null;
}
精彩评论