I want to be able to cancel a row selection in a DataGridView under certain circumstances but there doesn't seem to be an event that allows me to catch the selection change and cancel it, as there is when a user tries to delete a row. Am I missing something

Canceling DataGridView Selection Change
Jayson Speer
Sandhya
First correction - looks like just a slight coding issue -- your base.SetSelectedRowCore call needs to be outside the if block.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Second issue -- the DataGridView when selection changes calls ClearSelection which performs a loop to unselect all rows. The loop continues until all rows are deselected, so since your code above returns without clearing the row selection, the ClearSelection call will continue for ever, as long as the user ansers "no" to your messagebox.
Can you explain a bit more what your goal/scenario is
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"
andrew17
protected override void SetSelectedRowCore (int rowIndex, bool selected)
{
if (false == selected)
{
if (DialogResult.No == MessageBox.Show("Select a new record ", "New Record ", MessageBoxButtons.YesNo))
{
return;
}
base.SetSelectedRowCore(rowIndex, selected);
}
}
When I select "No," to cancel the selection change, the SetSelectedRowCore is continually called to deselect the first row. There is obviously code somewhere else trying to kill the old selection.
xx-Cougar-xx
FYI
OnRowValidating is also called whenever the grid loses focus. I.e. clicking outside the grid.
SetSelectedRowCore is only called when the row is changed. Well... called twice really but not when the grid loses focus.
But I never got subclassing and SetSelectedRowCore to work properly - the GUI showed a frame around the target row whatever I did.
If I recall correctly I had to use SetSelectedRowCore when programming Compact framework because it doesn't have OnRowValidating and then I got it to function propertly.
/OF
NorbertHH
I am having same problem but what i want is that once error occur say suppose user typed empty string for one of column value datagridview in below text box that i don't want user to change the row and also i want to retain the selection on the same row. currently i am setselectedrowcore event only.
Can you tell me how do i achieve
Thanks and Regards
K.Sathishkumar
Michael W.
I don't know why I didn't think of this the first time -- RowValidating would be the best way to do this:
protected override void OnRowValidating(DataGridViewCellCancelEventArgs e)
{
if (DialogResult.No == MessageBox.Show("Select a new record ", "New Record ", MessageBoxButtons.YesNo))
{
e.Cancel = true;
return;
}
else
base.OnRowValidating(e);
}
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"
Rebecca23
Dean100000000000000
No, there isn't any event to cancel row selection. Assuming you have FullRowSelect, you'll need to override SetSelectedRowCore and not call base for a specific row index. If the selection mode is not FullRowSelect then the cells in the row can still be selected, but the user can't click the row header and make the row appear selected.< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
-mark
DataGridView Program Manager
Microsoft
This post is provided "as-is"