Click to See Complete Forum and Search --> : How to retrieve the corect row in a data grid
Joel Romain
February 1st, 2005, 08:34 AM
Hello everybody,
Or the documentation is a deep jungle, or I begin to be older for this kind of sport !!
I try a basic sample of a grid filled from a dataset. Then I try to retrieve the record displayed on the row I "Double click". The following code
RR = AllData1.TheRisks.Rows(.CurrentRowIndex) works properly until .... I sort the grid
I've tried to retrieve data througth the currency manager :
myCurrencyManager = CType(Me.BindingContext(AllData1), CurrencyManager)
....
RR = AllData1.TheRisks.Rows(myCurrencyManager.Position)
without Success. I suppose this basic problem has many solutions but I need only one. Please help
Boumxyz2
February 1st, 2005, 11:03 AM
ok there'S a glitch with .NET Datagrids. the Datagrids never gets a double click because there's an embedded text control in your datagrid that actually gets the double click. To get the double click you need to destroy all the controls in the datagrids ( if you don't let the user modifiy the data that's ok ) or capture the double click from the textbox column in your datagrid.
I can't remember the correct link for that but I got the code to get rid of the controls.
DIM arrControl(2) As Control
' THIS WILL KEEP YOUR SCROLLBARS IN YOUR DATAGRID
Dim nbrbar As Integer = 0
For Each i As Control In Me.datasearch.Controls
If TypeOf i Is ScrollBar Then
arrControl(nbrbar) = i
nbrbar = nbrbar + 1
End If
Next
Me.datasearch.Controls.Clear()
Me.datasearch.Controls.AddRange(arrControl)
' FROM HERE THE DOUBLE CLICK ON DATAGRID WILL WORK
' BUT THE USER CAN'T MODIFY THE DATA
SatyaV
February 1st, 2005, 05:44 PM
Joel,
As Boumxyz2 posted, the Datagrid is really a collection of TextBox controls ... and those controls eat up any events before they reach the parent container(datagrid). Here is some code for determining rowcount for single-click on the grid : (grid name is grdClient)
Private Sub grdClient_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grdClient.MouseDown
Dim pt As New System.Drawing.Point()
pt = grdClient.PointToClient(Cursor.Position)
Dim ht As DataGrid.HitTestInfo = grdClient.HitTest(pt)
If ht.Type = DataGrid.HitTestType.Cell Then
nSelectedRow = ht.Row
End If
End sub
My guess is additionally you will need to capture events for other actions on the grid. For this you will have to wire event handlers for the contained textboxes. Some code that works for me(this example is for OnKeyDown, you can do this for other events too) :
Private Sub WireKeyPressHandlerForCells()
Dim t As DataGridTextBoxColumn
For Each t In grdClient.TableStyles(0).GridColumnStyles
AddHandler t.TextBox.KeyDown, AddressOf grdClient_KeyDown
Next
End Sub
Private Sub grdClient_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
If Not (e.KeyCode = Keys.Control) Then
//Do your action here
e.Handled = True
End If
End Sub
HTH
Joel Romain
February 2nd, 2005, 01:00 AM
Thank's but this is not my problem.
I've no problem NOR to catch the event, NOR to retrieve the hit row in the grid. Having the row, I can retrieve the corresponding record in the data set bounded to the grid IF THE GRID HAS NOT BEEN SORTED TILL IT HAS BEEN POPULATED. Once the grid has been sorted, I can't retrieve the correct record.
That's my problem
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.