WPF VB.Net Datagrid Value from a specific Cell in FullRow Selection Mode?

579 views Asked by At

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
1

There are 1 answers

4
D. Rego On

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.