Results 1 to 5 of 5

Thread: Why does Datagridview.CurrentCell.Selected transfer control out of the program?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Why does Datagridview.CurrentCell.Selected transfer control out of the program?

    It is the last statement in the sub so it really doesn't affect the function. But the CurrentCell.Selected statement transfers control out of the routine. Why? The ActiveControl / CurrentCell.Selected pair are there to remove the highlighting in the top left cell of the datagridview. It took me some googling to figure that out. (Most of the code samples I found didn't work.) If I comment out the CurrenCell.Selected control continues through to End Sub. I have included a few statements above for context. Is there another way to clear the highlighting?

    Code:
    Dim da As New OleDbDataAdapter(strSQL, dbConnection)
    Dim dt As New DataTable
    Try
       dbConnection.Open()
    Catch exc As OleDbException
       MsgBox(exc.Message)
    End Try
    da.Fill(dt)
    da.Dispose()
    DataGridView1.DataSource = dt
    
    dbConnection.Close()
    NumRecsTextBox.Text = CStr(dt.Rows.Count)
    
    dt = Nothing
    da = Nothing
    
    DataGridView1.Columns(0).Visible = False
    ActiveControl = ExitButton()
    DataGridView1.CurrentCell.Selected = False      <---------------- This statement transfers control out of the routine
    End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Why does Datagridview.CurrentCell.Selected transfer control out of the program?

    That would suggest that it's throwing an exception. I'd guess that CurrentCell is Nothing, thus it throws a NullReferenceException.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Why does Datagridview.CurrentCell.Selected transfer control out of the program?

    Yes, indeed, the error is Object reference not set to an instance of an object.

    Should I resume control or is there a better way? All I'm trying to do is clear the highlighting of the top left cell. DataGridView1.ClearSelection doesn't work.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Why does Datagridview.CurrentCell.Selected transfer control out of the program?

    Nothing will work if you're doing it in the Load event handler, which you must be doing if an exception was swallowed, because nothing is selected at that point. You need to do it in the Shown event handler, which is executed after the form is shown, rather than for Load, which is executed before the form is shown.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: Why does Datagridview.CurrentCell.Selected transfer control out of the program?

    Shown works. Thanks.

Posting Permissions

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



Click Here to Expand Forum to Full Width