Link to home
Start Free TrialLog in
Avatar of angireddy
angireddy

asked on

How can I select a row in the datagrid

How can I highlight the whole row in a datagrid when we click, the click can be right, left, double and also any coloumn. Please let me know how I can do this. Very urgent
Avatar of Howard Cantrell
Howard Cantrell
Flag of United States of America image

Here is a sample of clicking on a cell and recieving all info on each cell...


   Private Sub datagrid_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles datagrid.MouseUp
        Try
            txtQty.Text = dgInventory.Item(dgInventory.CurrentRowIndex, 2)        '<---- a text box to sent info to.
            txtDescription.Text = dgInventory.Item(dgInventory.CurrentRowIndex, 1) '<---- a text box to sent info to.
        Catch ex As InvalidCastException
            'Skip DBNULL
        Catch ex As SystemException
            MsgBox(ex.StackTrace & ex.Message, MsgBoxStyle.Critical, "General Error")
        End Try
    End Sub
Avatar of angireddy
angireddy

ASKER

No, I don't want to select the data from each cell. I want to highlight the whole row when we click on any coloumn of that row.
To highlight a row use the Select method...

            dgRecords.Select(dgRecords.CurrentRowIndex)

You can fire this from the click event which will include a double-click, right click etc...

Racin
My bad...

That doesn't work...

But this does in the MouseUp Event:

    Private Sub dgItems_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgItems.MouseUp

        Dim hti As System.Windows.Forms.DataGrid.HitTestInfo

        Try
            With dgItems
                hti = .HitTest(e.X, e.Y)

                If hti.Row >= 0 Then 'highlight row clicked on
                    .CurrentRowIndex = hti.Row
                    .Select(.CurrentRowIndex)
                End If

            End With

        Catch ex As Exception

            DisplayMessage.ErrorMessage("Error in MouseUp on Grid: " & ex.Message)

        End Try

    End Sub

Racin
ASKER CERTIFIED SOLUTION
Avatar of bramsquad
bramsquad
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oops, didnt know the answer before mine was there...

anyways, thought i should cite my work, got this from

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q689q
The code I gave you does not mean that you have to click on each cell.
Just the cell in that row and you can break downthe answer by selecting each cell value that you need.
Thanyou bramsquad, planocz and racinran
I'm curious... what was wrong with my anwser compared to bramsquad?
hahaha, don't get mad, even your answer is also correct. But both of your answers came at the same time probably and I was in a hurry to insert this code in my project. Both of your answers are the same. Sorry if I hurt you by accepting bramsquad answer.
not hurt ... just wanting to know ... I'm new here and am just trying to gain enough points to fully participate in the site.

No problem... :)

Racin