Solved

double click datagrid row to select row items

Posted on 2004-09-01
15
61,148 Views
Last Modified: 2011-08-18
how can i display the datagrid row items in to textboxes when double clicked on that row.
0
Comment
Question by:angireddy
15 Comments
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 125 total points
ID: 11953950
Something like this

    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
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11953960
This will copy when you double click on the rowheader
0
 
LVL 8

Expert Comment

by:bramsquad
ID: 11953964
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
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:angireddy
ID: 11954218
thank you, wants to know how you can make the datagrid non editable.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11961835
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...





0
 

Author Comment

by:angireddy
ID: 11962979
How to add menu when we right click on the datagrid, and the menu should be linked to the row of the datagrid
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11963037
This sounds like a complete new question. The rule here at EE is that you have to open a new question.
0
 

Author Comment

by:angireddy
ID: 11963065
sure i will add this as a new question, thank you and sorry
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11963072
No problem.
0
 

Author Comment

by:angireddy
ID: 11972749
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
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11972948
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
0
 
LVL 2

Expert Comment

by:GohdanTheMoblin
ID: 12151637
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.
0
 

Author Comment

by:angireddy
ID: 12152618
thank you
0
 

Expert Comment

by:BMcKD
ID: 12881844
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
0
 
LVL 1

Expert Comment

by:matthewsampson
ID: 13111477
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
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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