Click here to Skip to main content
14,738,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datagridview containing lot of data and I want to select data one by one using button.

dim i as integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       i += 1
       DataGridView1.Rows(i).Selected = True
End Sub


It's working, but when row out of DataGridView control, the selected row didn't auto focus.
(need scroll bar to see current selected)

How to set auto focus to selected row?
or
Is there any better code to make this work?
Posted
Updated 26-Jan-11 22:34pm
v2
Comments
Dalek Dave 27-Jan-11 3:34am
   
Edited for Grammar, Syntax and Readability.

I'm not an expert in VB but this must be accomplished something like this:
(Please note that the below code is C#)
DataGridView1.SelectedIndex = 1; //get the index of the row to set focus
DataGridView1.SelectedRow.Focus();
//Additionally, you can also change the background color of the row to grab the user's attention
//DataGridView1.SelectedRow.BackColor = System.Drawing.Color.Yellow;
   
An accurate answer depends on how you are binding your datagrid.

If you are binding via a BindingSource component you can use the BindingSource.MoveNext[^] BindingSource.MovePrev methods.

Or alternatively you might be able to use the CurrencyManager.Position[^] property to simulate that as shown in the example.
   
Comments
ikuto tohoin 27-Jan-11 19:11pm
   
I'm using DataTable as DataSource
I do not think that the datasource can control directly using binding, so I just focus edit my code datagridview.. haha

thank you it's work.. and I learned a lot from this
thank you for answer..
i'm also not expert in c#.. but i got error when run.. :)

DataGridView1.SelectedIndex = 1;


error say :
'SelectedIndex' is not a member of 'System.Windows.Forms.DataGridView' :(

i already try using SelectedIndex in other control and it's only work with Listbox or ComboBox control, and it's auto focus

i think using listbox as tabel view will be more complicate.. :sigh:
   
Comments
Henry Minute 27-Jan-11 10:13am
   
Please use the 'Add Comment' widget rather than adding a new answer. That way the answerer gets an Email notification.
ikuto tohoin 27-Jan-11 18:24pm
   
ok..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900