Canceling DataGridView Selection Change

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


Answer this question

Canceling DataGridView Selection Change

  • Jayson Speer

    Works great.  I had thought about Validation but my impression was that it was only for editable grids.  Thank you for taking the time to help.

  • 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

    I tried this, and it *almost* worked.  But I think I'm misusing SetSelectedRowCore:


    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

    hejdig.

    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

    Hi Mark,

    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.

    The DataGridView is read-only and contains rows from a database.  When a row is selected, the data populates into textboxes, comboboxes, radio buttons, etc. below the grid.  The user can then edit and save (or cancel changes to) the record.  If the user selects a different row in the grid with unsaved changes, I want to ask whether we should return to editing the old row, or throw away the changes and edit a new record.
    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

     Mark Rideout wrote:
    First correction - looks like just a slight coding issue -- your base.SetSelectedRowCore call needs to be outside the if block.
    Ah, yes.  Retyped it from memory.  Good catch. Big Smile
    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.
    OK, that's pretty close to what I thought was happening.  So I need to deal with this before SetSelectedRowCore is called.
    Can you explain a bit more what your goal/scenario is
    Sure.  The DataGridView is read-only and contains rows from a database.  When a row is selected, the data populates into textboxes, comboboxes, radio buttons, etc. below the grid.  The user can then edit and save (or cancel changes to) the record.  If the user selects a different row in the grid with unsaved changes, I want to ask whether we should return to editing the old row, or throw away the changes and edit a new record.

  • 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"

     


  • Canceling DataGridView Selection Change