0

Hi all,

I need my form to automatically select the first entry in the datagrid view when it is loaded. I am having trouble finding the correct way to code it.

Private Sub CustomersForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim cust = From customers In db.Customers Order By customers.CustName Select customers
        

        Me.DataGridView1.DataSource = cust
        Me.DataGridView1.SelectedCells() = '??? Do I assign this a value?  
        Dim custselect = DataGridView1.SelectedCells(1).Value
        Me.CustAccountsDataGridView.DataSource = From accounts In db.CustAccounts Where accounts.AccountNumber = custselect.ToString Select accounts

    End Sub

Any Ideas? TIA

4
Contributors
3
Replies
11
Views
7 Years
Discussion Span
Last Post by M.Waqas Aslam
0

I would suggest that you use the SelectedRows property.

Me.DataGridView1.SelectedRows = dataGridView1.Rows(0)

But test that there is at least one row first.

Edited by nick.crane: n/a

0

I would suggest that you use the SelectedRows property.

Me.DataGridView1.SelectedRows = dataGridView1.Rows(0)

But test that there is at least one row first.

hello how about the last row? can you give some code for retrieving the last row?

0

hello!
you can do like this harrispc

'put this code on the load event of the form
'before coding set selection property of grid to full row select
DataGridView1.Focus()
DataGridView1.Rows.GetFirstRow(DataGridViewElementStates.Visible)
'and shann1718 u can do like this         
DataGridView1.Rows.GetLastRow(DataGridViewElementStates.Visible)

hope this will helps you

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.