0

How can I get a Value from a specific Cell in a Datagrid with the Selection Mode FullRow ?

At the moment I use this command, but it only display's the Value if I manually select the cell:

Public Sub MenuItem_Click(sender As Object, e As RoutedEventArgs)
    Dim selectedData As String = ""
    Dim wert As String = ""
    For Each dataGridCellInfo In myGridView.SelectedCells()
        Dim pi As Reflection.PropertyInfo = dataGridCellInfo.Item.[GetType]().GetProperty(dataGridCellInfo.Column.Header.ToString())
        Dim value = pi.GetValue(dataGridCellInfo.Item, Nothing)
        selectedData += dataGridCellInfo.Column.Header + ": " + value.ToString() + vbLf
        wert = value.ToString
    Next
    MessageBox.Show(selectedData)

End Sub

But I don't want to manually select the cell all time. I just want to click on any cell in that Row and get always the value from the cell "Pfad" and put it in a String.

XAML DataGrid Code:

<DataGrid Name="myGridView" SelectionUnit="Cell" IsReadOnly="True" ItemsSource="{Binding}" Grid.ColumnSpan="3" Background="#FFA4A4A4" BorderThickness="2" BorderBrush="#FF6A6F77" AutoGenerateColumns="False" Margin="10,31,10,149.714" GridLinesVisibility="None">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Track" Binding="{Binding Path=Track}" Width="100"/>
            <DataGridTemplateColumn Header="">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Width="50" Height="50" VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding Path=Image}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn Binding="{Binding Path=Endung}" Width="100" Header=" Container"/>
            <DataGridTextColumn Binding="{Binding Path=Album}" Width="100" Header="Album"/>
            <DataGridTextColumn Binding="{Binding Path=Bitrate}" Width="100" Header="Bitrate"/>
            <DataGridTextColumn Binding="{Binding Path=Pfad}" Width="100" Header="Pfad"/>
        </DataGrid.Columns>
    </DataGrid>

Code-Behind:

    Public Structure Austauscher

    Dim files As New ObservableCollection(Of Austauscher)

    Private _track As String
    Private _image As BitmapImage
    Private _album As String
    Private _pfad As String
    Private _bitrate As String
    Private _endung As String


    Property Track() As String
        Get
            Return _track
        End Get
        Set(ByVal Value As String)
            _track = Value
        End Set
    End Property
    Property Image As BitmapImage
        Get
            Return _image
        End Get
        Set(ByVal Value As BitmapImage)
            _image = Value
        End Set
    End Property
    Property Album() As String
        Get
            Return _album
        End Get
        Set(ByVal Value As String)
            _album = Value
        End Set
    End Property
    Public Property Pfad As String
        Get
            Return _pfad
        End Get
        Set(ByVal Value As String)
            _pfad = Value
        End Set
    End Property
    Property Bitrate As String
        Get
            Return _bitrate
        End Get
        Set(ByVal Value As String)
            _bitrate = Value
        End Set
    End Property
    Property Endung As String
        Get
            Return _endung
        End Get
        Set(ByVal Value As String)
            _endung = Value
        End Set
    End Property

End Structure
xxXTimXxx
  • 3
  • 1

1 Answers1

0

At first you need tu open de event "CellClick" on your datagridview and put de following code:

 If (e.ColumnIndex <> -1) And (e.RowIndex <> -1) Then
       Dim selectedCell as string = ""
       selectedCell = myGridView.rows(e.rowIndex).cells("numberOfCell").valeu 
 End If
  • If this Answer has help you. Pleas mark this at the correct Answer.
  • Sorry, but I don't find any CellClick event in the WPF datagridview. – xxXTimXxx May 02 '16 at 15:02
  • Double click in datagrid and click in de dropdown to choise the event, and choise de cellClick – D. Rego May 02 '16 at 16:27
  • No I allready checked it out. In Windows Forms there is an event called CellClick, but not in WPF. You can check it out here too: [Microsoft Datagrid Event](https://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid_events(v=vs.110).aspx) – xxXTimXxx May 02 '16 at 17:30
  • Sorry man! I have maked a big confusion! I had not realized that is WPF, sorry man, in that case i cant help you :/ – D. Rego May 03 '16 at 09:19