I have 3 tabs with datagridviews. When the user adds a new record, then leaves the row, I call up the rowvalidating and ask the user if they want to discard the record that wasn't saved. If they say no, I use the e.cancel and it works great. It takes them back to the record, they can do whatever they want.
However, if they click on a different tab after inserting a new record, the e.cancel seems to mess up some of the mouse click events. I can click on the cells for inserted row, but the dropdown no longer expands, and I cannot highlight any text in cell, but I can type in the cells with no problem.
When I put a breakpoint on the datagridview_cellclick event, it doesn't event register that I clicked the cell.
Using VS2008 Pro
Dave
Loading
Suthish NairPosted Mar 31, 2011, 3:59 PM
Dave AbramPosted Apr 1, 2011, 9:31 AM
Created Boolean failedValidation = false;
So when it fails in the rowvalidating event, I set the failedValidation = true, then after the dialogresult answer of no, I set the failedValidation to true then call the UndoRowMove() using the Invoke.
dataGridView1.BeginInvoke( new MethodInvoker(UndoRowMove));
private void UndoRowMove()
{
if (failedValidation && dataGridView1.CurrentCell.Visible == true)
{
dataGridView1.CurrentCell = dataGridView1[currCellIndex, currRowIndex];
failedValidation = false;
}
}
Dave AbramPosted Mar 31, 2011, 9:21 PM
Create a new winforms applicationAdd a SplitPanel
Add a TabControl to one of the panels (default two tabs will work fine)
Add a DataGridView to one or both of the tabs (I have one on each tab)
Below for testing, I just wanted to make sure the current row that I was on and cell 4 had data in it. When I clicked on a different row with cell 4 empty, I get the messagebox, and the e.cancel will stop me from leaving the row and edit the text in the cell.
If you leave the cell empty, then click on the other tab, you get the message, it stays on the row, but no matter how much you click on the cell(s), you cannot get into edit mode. I know for a fact it has something to do with the Dialog MessageBox because an information messagebox work just fine.
It must be something to do with a Threading problem.
private void dataGridView1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
for (int i = 0; i < dataGridView1.Columns.Count; i++ )
if(i == 4)
{
if (dataGridView1[i, e.RowIndex].EditedFormattedValue.ToString().Length == 0)
{
DialogResult DialogResult = MessageBox.Show("Creating a new Contact Method line requires an entry in both the Contact Method and Contact Information fields." + Environment.NewLine + Environment.NewLine +
"Would you like to discard this record?: ", "Missing Required Data!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dlgResult == DialogResult.Yes){
// Delete the current record
}
else
{
e.Cancel = true;
}
}