Home All Groups Group Topic Archive Search About

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

Author
3 Apr 2006 2:19 PM
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()

Author
3 Apr 2006 5:56 PM
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

Show quoteHide quote
"Aziz" <aziz***@googlemail.com> wrote in message
news:1144073967.253355.112130@e56g2000cwe.googlegroups.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()
>
Author
4 Apr 2006 8:15 AM
Aziz
Thanks! Worked perfectly.

Any ideas on Q1?
Author
4 Apr 2006 4:04 PM
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

Show quoteHide quote
"Aziz" <aziz***@googlemail.com> wrote in message
news:1144138537.554366.153280@v46g2000cwv.googlegroups.com...
> Thanks! Worked perfectly.
>
> Any ideas on Q1?
>