By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

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

P: n/a
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()

Apr 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

"Aziz" <az*****@googlemail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
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()

Apr 3 '06 #2

P: n/a
Thanks! Worked perfectly.

Any ideas on Q1?

Apr 4 '06 #3

P: n/a
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/smartclien....aspx#8y225t5b

"Aziz" <az*****@googlemail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
Thanks! Worked perfectly.

Any ideas on Q1?

Apr 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.