none
DataGridView row automatically selected RRS feed

  • Question

  • This seems to be a really simple issue, but for some reason I've failed to find an answer. I have a DataGridView with MultiSelect = true and SelectionMode = FullRowSelect.

    I'm binding a business entity collection to the DataGridView. The collection may or may not be empty when it is set to the DataGridView DataSource, or objects may be added to the collection later. The problem I'm having is that as soon as the DataSource is set, the first row on the DataGridView is selected.

    I've done a few tests and it seems like no matter what happens with the DataGridView, as soon as something is bound to the data source something (first row or cell) will get selected regardless of the SelectionMode.


    Is there anyway to have nothing selected?
    Friday, October 6, 2006 12:51 AM

Answers

  • Have you tried DataGridView.ClearSelection() ?
    Friday, October 6, 2006 9:38 AM
  • Ok for some reason I had to put the DataGridView.ClearSelection() call in a Form_Shown(object sender, EventArgs e) event handler.

    My DataGridView sits in an MdiChild and gets its DataSource set when the form is displayed. Calling ClearSelection() after the DataSource is set didn't seem to do anything. I had to put it in the Form_Shown handler.

    But that seems to have solved the problem.
    Friday, October 6, 2006 9:34 PM

All replies

  • Have you tried DataGridView.ClearSelection() ?
    Friday, October 6, 2006 9:38 AM
  • Ok for some reason I had to put the DataGridView.ClearSelection() call in a Form_Shown(object sender, EventArgs e) event handler.

    My DataGridView sits in an MdiChild and gets its DataSource set when the form is displayed. Calling ClearSelection() after the DataSource is set didn't seem to do anything. I had to put it in the Form_Shown handler.

    But that seems to have solved the problem.
    Friday, October 6, 2006 9:34 PM
  • I was having the same issue... The only difference was that I have three DGV's total each on a different tab page.

    Your solution only worked for the first tab page...  I later figured out that if you added the following code:


    private void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
         ((DataGridView)sender).ClearSelection();
    }


    And then registered the DataBindingComplete event with each DGV it worked famously.

    Michael

    • Proposed as answer by Yann Duran Saturday, April 11, 2009 10:51 PM
    Thursday, November 2, 2006 3:10 PM
  • What can I do so the dataGridView_CurrentCellChanged event doesn't get triggered before I clear the selection?
    Monday, June 11, 2007 7:55 PM
  • Hi Michael,

    Thanks for sharing your solution!

    I had a similar situation with several DataGridViews on different tab pages & just using ClearSelection after setting the DataSource wasn't working.

    Your code allowed me to deselect the automatically selected row, just like I wanted to do.

    Thank you!

    Yann
    Saturday, April 11, 2009 10:54 PM
  • I was having the same issue... The only difference was that I have three DGV's total each on a different tab page.

    Your solution only worked for the first tab page...  I later figured out that if you added the following code:

     


     

    private void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
         ((DataGridView )sender).ClearSelection();
    }

     

     


     

    And then registered the DataBindingComplete event with each DGV it worked famously.

    Michael

    Good catch - that one worked for me as well!
    Thursday, October 1, 2009 3:18 PM
  • It all work OK if you use data binding but if you fill in the grid yourself it doesnt as the above event is never fired. You can use insted the Visible event:

     private void GridContractedShow_VisibleChanged( object sender, EventArgs e )
     {
         if (  ((DataGridView )sender) .Visible )
             ((DataGridView )sender) .ClearSelection( );

    }


    dgogoasa
    Monday, February 7, 2011 10:17 PM
  • Hi Dear vkh75

    Well Doneeeeeeeeeeeeeeeeeeeeeeee

    I toke time as a day and i found nothing but your solution working for me 

    Form_Shown()    Realy ?!!!! that was so simple :)

    thanks 

    Regards


    Thanks Before FARZAD KHAJEH

    Friday, November 22, 2019 1:14 PM