locked
disable Auto Select of DataGridView RRS feed

  • Question

  • hi

    I want to deselect the first row after filling the data each time with a datagridview

    use this code but not work

     Private Sub DATAGRID_SERVER_DataBindingComplete(sender As Object, e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DATAGRID_SERVER.DataBindingComplete
            On Error Resume Next
            DATAGRID_SERVER.ClearSelection()
     DATAGRID_SERVER.CurrentCell = Nothing
        End Sub


    • Edited by ahmeddc Sunday, September 30, 2018 6:18 PM vvv
    Sunday, September 30, 2018 6:16 PM

Answers

  • Try

    Private Sub DataGridView1_DataSourceChanged(sender As Object, e As EventArgs) Handles DataGridView1.DataSourceChanged
        DataGridView1.CurrentCell = Nothing
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Sunday, September 30, 2018 8:12 PM
    Sunday, September 30, 2018 7:38 PM

All replies

  • Hi

    Your

    DATAGRID_SERVER.ClearSelection

    is perfectly OK, and would work as expected. You must have something else causing a selection to be made after that line has been executed.

    And, .................. what is the

    On Error Resume Next

    all about? It is completely useless and redundant there. If you want to trap errors then better to use a Try...Catch block. That line itself could cause issues.


    Regards Les, Livingston, Scotland

    Sunday, September 30, 2018 7:31 PM
  • Try

    Private Sub DataGridView1_DataSourceChanged(sender As Object, e As EventArgs) Handles DataGridView1.DataSourceChanged
        DataGridView1.CurrentCell = Nothing
    End Sub


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by ahmeddc Sunday, September 30, 2018 8:12 PM
    Sunday, September 30, 2018 7:38 PM
  • I think this should do the trick

    ActiveControl = Nothing
    
    Depending on your version of VS you may have to reference "Me"
    Me.ActiveCOntrol = Nothing


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Sunday, September 30, 2018 7:57 PM