Link to home
Start Free TrialLog in
Avatar of GravityPilot
GravityPilot

asked on

How do I access individual cell data in a WPF Datagrid from a custom control?

I have created a button column in my wpf form, but I need to use this button to access information in the datagrid.  How do I access the data in certain cells specifically if the data is autogenerated?  For example, if I wanted to access the data in column 4, row 2?
I can bind the button's context to some column data, then access the data via the sender object in the code.  But what if I want the button content to be something totally different?  Is there some other part of the button that I can bind this info to?  Am I going about this the wrong way?  Any help would be appreciated.
<my:DataGrid Name="DataGrid2" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" SelectionUnit="Cell">
<my:DataGrid.Columns>
<my:DataGridTemplateColumn>
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>                                            <Button Content="{Binding Path=SomeColumnData}"/>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>


Private Sub myButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
    'Do something with sender.content
End Sub

Open in new window

Avatar of GravityPilot
GravityPilot

ASKER

That should be button.content, not button.context.....sorry.
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks CodeCruiser. I think I found what I needed from one of those links:

http://social.msdn.microsoft.com/Forums/en/wpf/thread/74332b78-6bfd-4ac9-af85-dfd9bec87a29

I modified Whitts code to get just what I needed.  Thanks to Whitts for that code as well!!

Private Sub myButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Dim depObj As DependencyObject = DirectCast(e.OriginalSource, DependencyObject)
        'Get to DataGridCell level
        While (depObj IsNot Nothing) AndAlso Not (TypeOf depObj Is DataGridCell) 'AndAlso Not (TypeOf depObj Is Primitives.DataGridColumnHeader)
            depObj = VisualTreeHelper.GetParent(depObj)
        End While

        If depObj Is Nothing Then
            MsgBox("Can't Find Grid Info")
            Exit Sub
        End If

        If TypeOf depObj Is DataGridCell Then
            Dim cell As DataGridCell = TryCast(depObj, DataGridCell)

            'Get to DataGridRow level
            While (depObj IsNot Nothing) AndAlso Not (TypeOf depObj Is DataGridRow)
                depObj = VisualTreeHelper.GetParent(depObj)
            End While

            If depObj Is Nothing Then
                Exit Sub
            End If

            Dim row As DataGridRow = TryCast(depObj, DataGridRow)

            Dim columnIndex As Integer = cell.Column.DisplayIndex
            Dim rowIndex As Integer = FindRowIndex(row)
            Try
                MsgBox(ExtractBoundValue(row, cell, "EmployeeCompID#").ToString)
            Catch ex As Exception
                MsgBox("DataGrid Indexing Error")
            End Try
        End If
    End Sub

    'Determine the index of a DataGridRow
    Private Function FindRowIndex(ByVal row As DataGridRow) As Integer
        Dim dataGrid As DataGrid = TryCast(ItemsControl.ItemsControlFromItemContainer(row), DataGrid)

        Dim index As Integer = dataGrid.ItemContainerGenerator.IndexFromContainer(row)

        Return index
    End Function

    Private Function ExtractBoundValue(ByVal row As DataGridRow, ByVal cell As DataGridCell, ByVal columnname As String) As Object

        Try

            ' find the object that is realted to this row
            Dim data As Object = row.Item

            ' extract the property value
            Dim properties As ComponentModel.PropertyDescriptorCollection = ComponentModel.TypeDescriptor.GetProperties(data)
            Dim [property] As ComponentModel.PropertyDescriptor = properties(columnname)
            Dim value As Object = [property].GetValue(data)

            Return value
        Catch ex As Exception
            Return "DataGrid Extraction Failed"
        End Try
    End Function

Open in new window

None of these links gave me exactly what I needed.  But they were enough to get me going in the right direction.