dcsimg
Results 1 to 4 of 4

Thread: Row Selected automatically While sorting the columns of Datagridview

  1. #1

    Thread Starter
    Member
    Join Date
    May 2019
    Posts
    39

    Row Selected automatically While sorting the columns of Datagridview

    Hi,

    When i sort the columns of the Datagridview, the first row of the datagridview is selected automatically, If i sort another column the previously selected row was displayed in the textboxes. I wrote this code to deselect the row, but its not working.

    Code:
      Private Sub DataGridView1_Sorted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Sorted
    
            DataGridView1.CurrentCell.Selected = False
    
        End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,754

    Re: Row Selected automatically While sorting the columns of Datagridview

    Do you understand what the difference between "current" and "selected" is? They are unrelated. If there are any rows at all in the grid then there will ALWAYS be one and only one current row. That is the row containing the current cell and that is the cell with input focus. Selected rows are those rows in the SelectedRows collection, each of which will be highlighted. There can be zero, one or more selected rows and the current row may or may not be selected. They are unrelated.

    Now, how about you provide a FULL and CLEAR explanation of the problem? What TextBoxes? Maybe an understanding of the relationship between those TextBoxes and the grid, e.g. both bound to the same DataTable, would help us understand the problem.

    When you say:
    the previously selected row was displayed in the textboxes
    what do you actually mean? Do you mean the data in the TextBoxes doesn't change even though the position of the grid row containing that data changes, or do you mean that the data changes to that in the grid row at the same position as before even though it's not the same record? Don't make us guess because we will either not bother or quite possibly guess wrong and waste our time and yours trying to solve the wrong problem.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,431

    Re: Row Selected automatically While sorting the columns of Datagridview

    Try this...

    Code:
    Private Sub DataGridView1_Sorted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Sorted
            DataGridView1.CurrentRow = Nothing
    End Sub

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,754

    Re: Row Selected automatically While sorting the columns of Datagridview

    Quote Originally Posted by .paul. View Post
    Try this...

    Code:
    Private Sub DataGridView1_Sorted(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Sorted
            DataGridView1.CurrentRow = Nothing
    End Sub
    That might be a challenge, given that CurrentRow is read-only. As I mentioned, the CurrentRow is the row containing the CurrentCell, so you'd have to set the CurrentCell to Nothing. I was under the impression that that was not possible but I just tested and it actually is possible.

    The code in post #1 actually deselect the current cell, but it is still the current cell, if you want there to be no current cell then you must assign Nothing to the CurrentCell property.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width