[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 485
  • Last Modified:

Vb.net 2008 DataGribview auto select

how to make Datagridview auto select when there is only 1 row of data.
0
aarontham
Asked:
aarontham
  • 3
  • 2
1 Solution
 
Fernando SotoCommented:
Hi aarontham;

The DataGridView should automatically select the first row by default. This is how it can be done
programmatically:

        If DataGridView1.Rows.Count = 1 Then
            DataGridView1.CurrentCell = DataGridView1.Item(0, 0)
        End If

Fernando
0
 
aaronthamAuthor Commented:
hi Guys

sorry for miss understand.
user will enter something on the search TextBox. it will query Database and display result on the datagridview. user will click on the datagridview. i use datagridview MouseClick event to fire. and it will display result into few label.

how can i programmatically automate it when the search result only have 1 row in datagridbiew and it will auto display result into few label.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CodeCruiserCommented:
If there is only 1 row in the search result, you can call the label population method manually. Put the code that sets the labels into a Sub, then use that sub in the selection change event. After performing search, if there is only one row then call that sub automatically.
0
 
aaronthamAuthor Commented:
Hi CodeCruiser,

can you help to provide some sample vb.net code?
0
 
CodeCruiserCommented:
Hi,
You would use something like the following code

Grid_SelectionChangedEvent:
LoadLabels()

btnSearch_Click:
'code for performing search
...
if grid.rows.count = 1 then
   LoadLabels()
end if

Private Sub LoadLabels()
    If grid.selectedrows.count = 1 then
       lblName.text = grid.selectedrows(0).Cells("Name")
       lblAddress.text = grid.selectedrows(0).Cells("Address")
    end if
End Sub
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now