读取WPF datagrid

时间:2016-06-09 20:16:44

标签: wpf vb.net datagridview

我有一个名为“dgvresults”的数据网格,我在代码隐藏中向此数据网格添加列,如下所示。

Dim column_selected As New DataGridCheckBoxColumn()
column_selected.Header = "Selected"
column_selected.Binding = New Binding("IsChecked")
dgvResults.Columns.Add(column_selected)

Dim column_username As New DataGridTextColumn()
column_username.Header = "User Name"
column_username.Binding = New Binding("UserName")
dgvResults.Columns.Add(column_username)

完成此操作后,我使用它来添加行。

Private Sub run_click(sender As Object, e As RoutedEventArgs)

 For Each item In SearchFunctions.Usersearch
            'addrows(item)

            users.Add(New User With { _
                 .UserName = item
            })

        Next

        dgvResults.ItemsSource = users

 End Sub

该课程看起来像这样

Imports System.ComponentModel


Public Class User

    Implements INotifyPropertyChanged

    Public Property UserName() As String
        Get
            Return m_UserName
        End Get
        Set(value As String)
            m_UserName = value
        End Set
    End Property
    Private m_UserName As String


    Public Property IsChecked() As Boolean
        Get
            Return _IsChecked
        End Get
        Set(value As Boolean)
            _IsChecked = value
            NotifyPropertyChanged("IsChecked")
        End Set
    End Property
    Private _IsChecked As Boolean

    'Public ReadOnly Property Details() As String
    '    Get
    '        Return [String].Format("{0} was born on {1} and this is a long description of the person.", Me.Name, Me.Birthday.ToLongDateString())
    '    End Get
    'End Property

#Region "INotifyPropertyChanged Members"

    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

#End Region

#Region "Private Helpers"

    Private Sub NotifyPropertyChanged(propertyName As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub

#End Region

End Class

最终结果看起来有点像......

enter image description here

我目前遇到的问题是,如何找到数据网格中的所有已检查项目,并逐列读取每一行。

任何建议都会有所帮助!

谢谢!

2 个答案:

答案 0 :(得分:1)

我是stackoverflow中的新手:-),不是这方面的专家,但为什么你不能迭代用户(数据源)列表(foreach或使用linq)来查找已检查的。 datagrid仅显示您的数据源。

答案 1 :(得分:0)

刚回答我自己的问题,谢谢:)

For Each item In users
    If item.IsChecked Then
        MessageBox.Show(item.UserName)
    End If
Next