积极答复者
Wpf DataGrid 怎么获取当前行中的某一个数据(VB.NET)

问题
答案
-
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.- 已标记为答案 Aileen--1024 2018年10月24日 11:51
全部回复
-
使用 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/
- 已编辑 Bill ChungMVP 2018年10月23日 13:11
-
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.- 已标记为答案 Aileen--1024 2018年10月24日 11:51