Getting DataGrid row X, Y position and Changing Column Width (2003)

A

Aziz

1. I have a shopping basket DataGrid with a list of products. What I
want to do is when the user clicks on a row, a button will become
visible/be created that allows user to edit the quantity. The button
will be dynamic and always show up on the same Y-axis position as the
selected row.
So, how do I get back the X, Y co-ordinates of a specific row in a
datagrid relative to the form?

2. I managed to change individual column width using:

dgrProductSearch.DataSource = dtProductSearch

'Sets DataGrid column width individually
Dim dgtsProductSearch As DataGridTableStyle = New
DataGridTableStyle

dgtsProductSearch.MappingName = "ProductSearch"
dgrProductSearch.TableStyles.Add(dgtsProductSearch)

dgtsProductSearch.GridColumnStyles(0).Width = 90
dgtsProductSearch.GridColumnStyles(1).Width = 410
dgtsProductSearch.GridColumnStyles(2).Width = 90

This code is attached to a Search button that will display the relevant
product in the DataGrid.


However, if the button is pressed and the code executes again I get:
"The data grid table styles collection already contains a table style
with the same mapping name"

If I add this to the code above I don't get the above error but niether
does the column width change:

dgtsProductSearch.MappingName = ""
dgtsProductSearch.Dispose()
 
J

Jim Hughes

Only add the DataGridTableStyle if it is Nothing

' returns nothing if it does not exist
Dim dgtsProductSearch As DataGridTableStyle =
Me.dgrProductSearch.TableStyles("ProductSearch")
If dgtsProductSearch Is Nothing Then
dgtsProductSearch = New DataGridTableStyle()
dgtsProductSearch.MappingName = "ProductSearch"
dgrProductSearch.TableStyles.Add(dgtsProductSearch)
End If
dgtsProductSearch.GridColumnStyles(0).Width = 90
dgtsProductSearch.GridColumnStyles(1).Width = 410
dgtsProductSearch.GridColumnStyles(2).Width = 90
 
J

Jim Hughes

I don't know if this will work in all situations, but it's worth a shot.

You can use the DataGrid's HitTest method, passing it a point in the grid's
client coordinate system, and returning a HitTestInfo object that holds all
the row and column information that you want.

X & Y are in the grid' coordinates. If they are in screen coordinates, call
dataGrid1.PointToClient method

Dim pt = New Point(X, Y)
Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(pt)
If hti.Type = DataGrid.HitTestType.Cell Then
MessageBox.Show(dataGrid1(hti.Row, hti.Column).ToString())
Else
If hti.Type = DataGrid.HitTestType.ColumnHeader Then
'assumes datasource is a dataview
MessageBox.Show( _
CType(DataGrid1.DataSource,
DataView).Table.Columns(hti.Column).ToString())
End If
End If

From FAQ at
http://msdn.microsoft.com/smartclient/community/wffaq/ctrlsp.aspx#8y225t5b
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top