How to make the datagrid view not hightlight the first row?

B

bill

When you retrieve records to diplay in a DataGridView control the first
record has the background highlighted even though no row is yet selected.
Does anyone know a way to suppress this first record highlighting after
pulling the records into the grid?
 
J

Jack Jackson

When you retrieve records to diplay in a DataGridView control the first
record has the background highlighted even though no row is yet selected.
Does anyone know a way to suppress this first record highlighting after
pulling the records into the grid?

Your description isn't quite correct. The first row is in fact
selected - if you look at DataGridView.SelectedRows you should see the
first row in the list.

I don't know of any way to prevent the first row from being selected.
You have to arrange to unselect it after the DataGridView selects it.

If the datagridview is visible at the time you set it's DataSource,
then calling DataGridView.ClearSelection() immediately after setting
the DataSource will most likely work.

If you are setting the DataSource during the construction of a form
before the Load event fires, then you may have to call ClearSelection
later, perhaps in the Form.Load event.
 
B

bill

The problem I see it that the little chevron doesn't show to the left of the
highlighted row. It is highlighted but until that chevron is there if you
try to reference the data set in that row it errors out. I can't seem to
test for it either.
 
J

Jack Jackson

I am not sure how you "test for it".

The DataGridView has two separate things, selected rows (the
SelectedRows collection) and CurrentRow, the current row. The marker
in the row header (the left-most column) marks the current row. You
can change which row is current by setting the CurrentCell property to
a cell in the row you wish to make current.

I don't understand what you mean by "if you try to reference the data
set in that row it errors out". If you get an error, please always
say what the error is. How are you trying to reference the data?
 
K

Kevin S Gallagher

Before testing try


IsNothing(MyDataGridView.CurrentRow)



This should at least stop u from accessing a row that is not valid at the
current moment in time.
 
B

bill

Excellent to know. Thank you!

Kevin S Gallagher said:
Before testing try


IsNothing(MyDataGridView.CurrentRow)



This should at least stop u from accessing a row that is not valid at the
current moment in time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top