Solved

double click datagrid row to select row items

Posted on 2004-09-01
15
61,142 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now