Solved

Vb.net 2008 DataGribview auto select

Posted on 2009-04-12
6
476 Views
Last Modified: 2013-11-26
how to make Datagridview auto select when there is only 1 row of data.
0
Comment
Question by:aarontham
  • 3
  • 2
6 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 24125770
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24125773
0
 

Author Comment

by:aarontham
ID: 24128092
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 24135846
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
 

Author Comment

by:aarontham
ID: 24137758
Hi CodeCruiser,

can you help to provide some sample vb.net code?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 24145951
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question