none
DatagridComboBoxColumn Selected Value RRS feed

  • Question

  • Hello Experts,

    I have a WPF app with VB.Net code-behind. I need some help with getting the selected value of a combobox in adatagrid's selected row. The snippet below works great with textcolumns, but not with combobox columns:

    Dim row As System.Data.DataRowView = CType(MyDatagrid.SelectedItem, System.Data.DataRowView)

    Dim val = row.item("TextColumn")

    So I tried

    Dim val = row.item("ComboColumn").SelectedValue

     but it throws a "missing member" exception.

    Any help would be greatly appreciated,

    Thank you!

    Thursday, August 15, 2019 6:22 AM

Answers

  • Hi,

    The definition class implements the INotifyPropertyChanged interface to notify the UI that the property value has changed:

    vb.net code:

    Imports System.ComponentModel
    
    Class MainWindow
        Public Class MyModel
            Implements INotifyPropertyChanged
    
            Public Event PropertyChanged As PropertyChangedEventHandler
            Private Event INotifyPropertyChanged_PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
            Private _id As Object
    
            Public Property ID As Object
                Get
                    Return _id
                End Get
                Set(ByVal value As Object)
                    _id = value
                    Me.OnPropertyChanged(New PropertyChangedEventArgs("ID"))
                End Set
            End Property
    
            Private _selectedItem As Object
    
            Public Property SelectedItem As Object
                Get
                    Return _selectedItem
                End Get
                Set(ByVal value As Object)
                    _selectedItem = value
                    Me.OnPropertyChanged(New PropertyChangedEventArgs("SelectedItem"))
                End Set
            End Property
    
            Public Overridable Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
                RaiseEvent PropertyChanged(Me, e)
            End Sub
        End Class
        Public Property MyDataGridItems As List(Of MyModel)
        Public Property MaxThreshold As List(Of Object)
        Public Sub New()
            InitializeComponent()
            MyDataGridItems = New List(Of MyModel)() From {
          New MyModel() With {
              .ID = 1
          },
          New MyModel() With {
              .ID = 2
          },
          New MyModel() With {
              .ID = 3
          },
          New MyModel() With {
              .ID = 4
          },
          New MyModel() With {
              .ID = 5
          }
      }
            MaxThreshold = New List(Of Object)() From {
                "Item 1",
                "Item 2",
                "Item 3",
                "Item 4",
                "Item 5"
            }
        End Sub
    
        Private Sub Btn1_Click(sender As Object, e As RoutedEventArgs)
            Dim model = TryCast(DataGrid1.Items(DataGrid1.SelectedIndex), MyModel)
    
            If model IsNot Nothing Then
                MsgBox(model.SelectedItem)
            End If
        End Sub
    
    End Class

    Xaml code:

     <StackPanel>
            <DataGrid x:Name="DataGrid1" AutoGenerateColumns="False" 
                      ItemsSource="{Binding MyDataGridItems,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}">
                <DataGrid.Columns >
                    <DataGridTextColumn Header="ID"  
                            Binding="{Binding ID}" 
                            />
                    <DataGridTemplateColumn Header="SelectedItem">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="Threshold" 
                                          SelectedItem="{Binding SelectedItem, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                          ItemsSource="{Binding MaxThreshold,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    
                </DataGrid.Columns>
            </DataGrid>
            <Button Content="btn1" Name="btn1" Margin="5" Click="Btn1_Click"/>
        </StackPanel>

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, August 16, 2019 2:45 AM
    Moderator

All replies

  • Hi,

    The definition class implements the INotifyPropertyChanged interface to notify the UI that the property value has changed:

    vb.net code:

    Imports System.ComponentModel
    
    Class MainWindow
        Public Class MyModel
            Implements INotifyPropertyChanged
    
            Public Event PropertyChanged As PropertyChangedEventHandler
            Private Event INotifyPropertyChanged_PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
            Private _id As Object
    
            Public Property ID As Object
                Get
                    Return _id
                End Get
                Set(ByVal value As Object)
                    _id = value
                    Me.OnPropertyChanged(New PropertyChangedEventArgs("ID"))
                End Set
            End Property
    
            Private _selectedItem As Object
    
            Public Property SelectedItem As Object
                Get
                    Return _selectedItem
                End Get
                Set(ByVal value As Object)
                    _selectedItem = value
                    Me.OnPropertyChanged(New PropertyChangedEventArgs("SelectedItem"))
                End Set
            End Property
    
            Public Overridable Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)
                RaiseEvent PropertyChanged(Me, e)
            End Sub
        End Class
        Public Property MyDataGridItems As List(Of MyModel)
        Public Property MaxThreshold As List(Of Object)
        Public Sub New()
            InitializeComponent()
            MyDataGridItems = New List(Of MyModel)() From {
          New MyModel() With {
              .ID = 1
          },
          New MyModel() With {
              .ID = 2
          },
          New MyModel() With {
              .ID = 3
          },
          New MyModel() With {
              .ID = 4
          },
          New MyModel() With {
              .ID = 5
          }
      }
            MaxThreshold = New List(Of Object)() From {
                "Item 1",
                "Item 2",
                "Item 3",
                "Item 4",
                "Item 5"
            }
        End Sub
    
        Private Sub Btn1_Click(sender As Object, e As RoutedEventArgs)
            Dim model = TryCast(DataGrid1.Items(DataGrid1.SelectedIndex), MyModel)
    
            If model IsNot Nothing Then
                MsgBox(model.SelectedItem)
            End If
        End Sub
    
    End Class

    Xaml code:

     <StackPanel>
            <DataGrid x:Name="DataGrid1" AutoGenerateColumns="False" 
                      ItemsSource="{Binding MyDataGridItems,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}">
                <DataGrid.Columns >
                    <DataGridTextColumn Header="ID"  
                            Binding="{Binding ID}" 
                            />
                    <DataGridTemplateColumn Header="SelectedItem">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox x:Name="Threshold" 
                                          SelectedItem="{Binding SelectedItem, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                                          ItemsSource="{Binding MaxThreshold,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" />
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
    
                </DataGrid.Columns>
            </DataGrid>
            <Button Content="btn1" Name="btn1" Margin="5" Click="Btn1_Click"/>
        </StackPanel>

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, August 16, 2019 2:45 AM
    Moderator
  • Thank you, Alex!
    Monday, August 19, 2019 11:53 PM