none
Wpf DataGrid 怎么获取当前行中的某一个数据(VB.NET) RRS feed

  • 问题

  • 大家好:

    以前我是用Winform进行开发的,但为了适应不同分辨率转向了WPF。在Winform中有这么一个语句:

    TextBox1.Text = DataGridView1.CurrentRow.Cells.Item(0).Value.ToString
    

    这个语句返回DataGridView1当前所选行的列0的内容,但是WPF的DataGrid没有CurrentRow属性。各种链接数据准备都完成了。我想要实现这个功能,在WPF里面应该怎么做?

    谢谢

    2018年10月23日 12:03

答案

  • Hi   Aileen--1024,

    在WPF中,单击DataGrid,你可以通过点击当前行获取当前行的数据,事实上获取的选中行是一个DataRowView,你可以通过以下的方法来获取选中行的数据.

    var a =this.exDataGrid.selectItem;
    var b= a as DataRowView;
    

    如果你的DataGrid的ItemsSource是对象集合而不是DataTable的话,那么使用DataGrid.SelectedItem as YouClass就可以获得。

    下面一个小例子:

    XAML:

    <Grid>
            <DataGrid x:Name="dgbing" ItemsSource="{Binding}"
                AutoGenerateColumns="False" Margin="0,0,0,107"   >
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="EmployeeName" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding EmployeeName}"  ></TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Header="Colorsvalue" Binding="{Binding Colorsvalue}" x:Name="Colorsvalue"/>
                </DataGrid.Columns>
            </DataGrid>
            <Button Content="Button" HorizontalAlignment="Left" Margin="220,340,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
        </Grid>
    

    XAML.VB:

    Public Class datagridtestdemo
    	Private jb2 As ObservableCollection(Of EmployeeColorT) = New ObservableCollection(Of EmployeeColorT)()
    
    	Public Sub New()
    		InitializeComponent()
    		jb2.Add(New EmployeeColorT() With {
    			.EmployeeName = "Job1",
    			.Colorsvalue = "1"
    		})
    		jb2.Add(New EmployeeColorT() With {
    			.EmployeeName = "Job2",
    			.Colorsvalue = "2"
    		})
    		jb2.Add(New EmployeeColorT() With {
    			.EmployeeName = "Job3",
    			.Colorsvalue = "3"
    		})
    		dgbing.DataContext = jb2
    	End Sub
    
    
    	Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
    		Dim mySelectedElement As EmployeeColorT = dgbing.SelectedItem
    		MessageBox.Show(mySelectedElement.EmployeeName)
    	End Sub
    
    
    End Class
    
    
    Public Class EmployeeColorT
    
    
    	Private _EmployeeName As String = String.Empty
    
    	Public Property EmployeeName As String
    		Get
    			Return _EmployeeName
    		End Get
    		Set(ByVal value As String)
    
    			If _EmployeeName <> value Then
    				_EmployeeName = value
    
    			End If
    		End Set
    	End Property
    
    	Private _colorsvalue As String = String.Empty
    
    	Public Property Colorsvalue As String
    		Get
    			Return _colorsvalue
    		End Get
    		Set(ByVal value As String)
    
    			If _colorsvalue <> value Then
    				_colorsvalue = value
    
    			End If
    		End Set
    	End Property
    
    	Public Sub New()
    	End Sub
    
    End Class
    
    Best Regards,

    Yong Lu


    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.

    2018年10月24日 5:53
    版主

全部回复

  • 使用 DataGird.SelectedItem 屬性比較接近 Windows Forms 的 DataGirdView.CurrentRow

    參考:
    https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.controls.primitives.selector.selecteditem?view=netframework-4.7.2#System_Windows_Controls_Primitives_Selector_SelectedItem


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。 https://skilltree.my/



    2018年10月23日 13:10
  • Hi   Aileen--1024,

    在WPF中,单击DataGrid,你可以通过点击当前行获取当前行的数据,事实上获取的选中行是一个DataRowView,你可以通过以下的方法来获取选中行的数据.

    var a =this.exDataGrid.selectItem;
    var b= a as DataRowView;
    

    如果你的DataGrid的ItemsSource是对象集合而不是DataTable的话,那么使用DataGrid.SelectedItem as YouClass就可以获得。

    下面一个小例子:

    XAML:

    <Grid>
            <DataGrid x:Name="dgbing" ItemsSource="{Binding}"
                AutoGenerateColumns="False" Margin="0,0,0,107"   >
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="EmployeeName" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding EmployeeName}"  ></TextBlock>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Header="Colorsvalue" Binding="{Binding Colorsvalue}" x:Name="Colorsvalue"/>
                </DataGrid.Columns>
            </DataGrid>
            <Button Content="Button" HorizontalAlignment="Left" Margin="220,340,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
        </Grid>
    

    XAML.VB:

    Public Class datagridtestdemo
    	Private jb2 As ObservableCollection(Of EmployeeColorT) = New ObservableCollection(Of EmployeeColorT)()
    
    	Public Sub New()
    		InitializeComponent()
    		jb2.Add(New EmployeeColorT() With {
    			.EmployeeName = "Job1",
    			.Colorsvalue = "1"
    		})
    		jb2.Add(New EmployeeColorT() With {
    			.EmployeeName = "Job2",
    			.Colorsvalue = "2"
    		})
    		jb2.Add(New EmployeeColorT() With {
    			.EmployeeName = "Job3",
    			.Colorsvalue = "3"
    		})
    		dgbing.DataContext = jb2
    	End Sub
    
    
    	Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
    		Dim mySelectedElement As EmployeeColorT = dgbing.SelectedItem
    		MessageBox.Show(mySelectedElement.EmployeeName)
    	End Sub
    
    
    End Class
    
    
    Public Class EmployeeColorT
    
    
    	Private _EmployeeName As String = String.Empty
    
    	Public Property EmployeeName As String
    		Get
    			Return _EmployeeName
    		End Get
    		Set(ByVal value As String)
    
    			If _EmployeeName <> value Then
    				_EmployeeName = value
    
    			End If
    		End Set
    	End Property
    
    	Private _colorsvalue As String = String.Empty
    
    	Public Property Colorsvalue As String
    		Get
    			Return _colorsvalue
    		End Get
    		Set(ByVal value As String)
    
    			If _colorsvalue <> value Then
    				_colorsvalue = value
    
    			End If
    		End Set
    	End Property
    
    	Public Sub New()
    	End Sub
    
    End Class
    
    Best Regards,

    Yong Lu


    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.

    2018年10月24日 5:53
    版主