Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get row data with a left mouse click

Status
Not open for further replies.

Ziggurat

Programmer
Jun 6, 2001
81
GB
Hi all

I am wondering if anyone can give me an answer to this problem.

I have a DataGrid on a form with data inside it.

I would like to left click on a row and have all the information in that row copied into the appropriate text boxes which are on the same form, under the DataGrid.

I seems like it should be so easy but I can't find a solution.


Thanks in Advance

Ziggurat
 
If the DataGrid is bound to the DataSet, then the best approach is to add an event handler for the BindingContext's CurrentChanged event.

Ex:
AddHandler BindingContext(DataSet1, "table").CurrentChanged, AddressOf table_CurrentChanged

Private Sub table_CurrentChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim bm As BindingManagerBase = CType(sender, BindingManagerBase)
'bm.Current.row is current datarow that grid has selected
End Sub

If you don't have the DataGrid bound to a DataSet, then you can trap when the current cell changes in the grid. At that point you'll have to manually copy all the cells to the textboxes. You would want to do this over the left mouse button, becaue it'll track when they change rows using the arrow keys, pgup, etc. also.

The easiest solution (I would think) is to use a DataSet and have all the textboxes and the DataGrid bound to the same DataSet/Member. Then as they change data in they move around the grid, the textboxes change automagically and as they change the text in one, it shows up the other, etc. You don't have to track anything, you just let the framework do all the work.
 
Hi FRoeCassNet

As I am a relative newcommer to VB.NET. I don't understand the principle of BindingContext and binding to DataSet/Members.

Also where do you enter the line :-
AddHandler BindingContext(DataSet1, "table").CurrentChanged, AddressOf table_CurrentChanged

If possible could you show me or point me to somewhere that I can see an example of this binding at work.

Thanks for your time

Ziggurat
 
Try this. Create a form (try wizard) that shows a single data row from your table, and then add a DataGrid to your project. Then, to view all records in a datagrid, load dataset and perform bind - DataGrid1.SetDataBinding(yourdataset,"yourtable"). This will show all your records in grid. Code is simple, add a new sub :
Private Sub DataGrid1_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
Me.BindingContext(yourdataset, "yourtable").Position = DataGrid1.CurrentRowIndex
Me.yourdataset_PositionChanged()
End Sub
Me.yourdataset_PositionChanged is a wizard-created sub. Check it's code.[morning]
 
Thank you Legba

I will try it.

Long Live Tek-Tips !!!!

Ziggurat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top