|
-
May 10th, 2016, 08:52 PM
#1
Thread Starter
Fanatic Member
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
-
May 10th, 2016, 09:14 PM
#2
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.
-
May 10th, 2016, 09:37 PM
#3
Thread Starter
Fanatic Member
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.
-
May 11th, 2016, 01:39 AM
#4
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.
-
May 11th, 2016, 11:27 AM
#5
Thread Starter
Fanatic Member
Re: Why does Datagridview.CurrentCell.Selected transfer control out of the program?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|