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

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.

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?

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.