Link to home
Start Free TrialLog in
Avatar of angireddy
angireddy

asked on

double click datagrid row to select row items

how can i display the datagrid row items in to textboxes when double clicked on that row.
ASKER CERTIFIED SOLUTION
Avatar of RonaldBiemans
RonaldBiemans

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
Avatar of RonaldBiemans
RonaldBiemans

This will copy when you double click on the rowheader
this'll get you started...what this code does, is sends the double-clicked cell value to the textbox.

youll want to use a for loop and loop through the number of rows you have in order to get them all

    Private Sub DataGrid1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.DoubleClick
        Me.TextBox1.Text = Me.DataGrid1.Item(DataGrid1.CurrentCell.RowNumber, DataGrid1.CurrentCell.ColumnNumber)
    End Sub

~b
Avatar of angireddy

ASKER

thank you, wants to know how you can make the datagrid non editable.
There are several ways

datagrid1.readonly = true or

adjust the underlaying dataview

datagrid1.datasource = yourdataview
yourdataview.Allownew = false
yourdataview.Allowedit = false
yourdataview.Allowdelete = false

here you can see that is full customizable

you can allow edit but not delete and add or just allow add and delete but not edit etc...





How to add menu when we right click on the datagrid, and the menu should be linked to the row of the datagrid
This sounds like a complete new question. The rule here at EE is that you have to open a new question.
sure i will add this as a new question, thank you and sorry
No problem.
Hi RonaldBiemans,

The code you gave me worked well for some time, then started to give problems. The if statement "if hti.type = Datagrid.hitTestType.rowheader" is always false and the rowheader is showing as constant and the value is 4. Please let me know what I can do to correct the problem.

Private Sub DataGrid1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.DoubleClick
        Dim pt As System.Drawing.Point = DataGrid1.PointToClient(Cursor.Position)

        Dim hti As DataGrid.HitTestInfo = DataGrid1.HitTest(pt)
        If hti.Type = DataGrid.HitTestType.RowHeader Then
            TextBox1.Text = CType(DataSet11.Tables(0).Rows(hti.Row).Item("yourcolumn"), String)
        End If

    End Sub
Well, the rowheader is always 4 because it is a constant, the problem lies with the hti.type which is somehow never 4.

Have you change something else in the datagrid, because a programs behaviour doesn't just change by itself so you must have changed something that has influenced this behaviour
The DataGrid's double-click event is tricky.  The DoubleClick event is only fired if you click on some portion of the DataGrid control that does not have a cell.  Otherwise, the first click is caught by the DataGrid and activates the cell, and the second click is caught by the cell.

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q869q
The above link (the anchor isn't working right in Firefox, it is item 5.46 on the page) has a project that properly catches a double-click in a DataGrid cell.  From within the double-click function, you can use the DataGrid.CurrentRowIndex function to figure out what row was clicked on, and use the DataGrid.Item property to fetch whatever values from the row you want.
thank you
Alternatively, you can disable the check boxes within the Datagrid, then the doubleclick is passed to the container.

Dim ctl As Control
For Each ctl In dgridMain.Controls
Dim gd As new System.Windows.Forms.DataGridTextBox
If ctl.GetType Is gd.GetType Then
ctl.Enabled = False
End If
Next

The grid will then respond to a double click in cells as well as the row header.

Solution coutesy -Banmere
Hi Guys,

I've been following this so far and it's all working great, so many thanks for posting it all. I'm quite new to all this so it's really helpful to read all the responses.

The problem I have is once I've edited the data in the textboxes, How do you get the data to go back into the datagrid?

Any help would be great.

Many Thanks


Matt Sampson
--------------------
Anglia University, Cambridge