在DataGrid WPF中获取选定的行项目

我有一个DataGrid ,绑定到数据库表,我需要获取DataGrid中选定行的内容,例如,我想在MessageBox显示所选行的内容。

DataGrid例子:

在这里输入图像描述

所以,如果我select第二行,我的MessageBox必须显示如下: 646 Jim Biology

您可以使用SelectedItem属性来获取当前选定的对象,然后将其转换为正确的types。 例如,如果你的DataGrid绑定到一个Customer对象的集合,你可以这样做:

 Customer customer = (Customer)myDataGrid.SelectedItem; 

或者,您可以将SelectedItem绑定到源类或ViewModel。

 <Grid DataContext="MyViewModel"> <DataGrid ItemsSource="{Binding Path=Customers}" SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"/> </Grid> 

如果您使用的是MVVM模式,则可以将VM的SelectedRecord属性与DataGrid的SelectedItem绑定,这样您就可以始终拥有VM中的SelectedValue 。 否则,您应该使用DataGrid的SelectedIndex属性。

 public IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid) { var itemsSource = grid.ItemsSource as IEnumerable; if (null == itemsSource) yield return null; foreach (var item in itemsSource) { var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow; if (null != row) yield return row; } } private void DataGrid_Details_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { var row_list = GetDataGridRows(DataGrid_Details); foreach (DataGridRow single_row in row_lis) { if (single_row.IsSelected == true) { MessageBox.Show("the row no."+single_row .GetIndex ().ToString ()+ " is selected!"); } } } catch { } } 

那么我会把类似的解决scheme,为我工作得很好。

  private void DataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { if (DataGrid1.SelectedItem != null) { if (DataGrid1.SelectedItem is YouCustomClass) { var row = (YouCustomClass)DataGrid1.SelectedItem; if (row != null) { // Do something... // ButtonSaveData.IsEnabled = true; // LabelName.Content = row.Name; } } } } catch (Exception) { } } 
 private void Fetching_Record_Grid_MouseDoubleClick_1(object sender, MouseButtonEventArgs e) { IInputElement element = e.MouseDevice.DirectlyOver; if (element != null && element is FrameworkElement) { if (((FrameworkElement)element).Parent is DataGridCell) { var grid = sender as DataGrid; if (grid != null && grid.SelectedItems != null && grid.SelectedItems.Count == 1) { //var rowView = grid.SelectedItem as DataRowView; try { Station station = (Station)grid.SelectedItem; id_txt.Text = station.StationID.Trim() ; description_txt.Text = station.Description.Trim(); } catch { } } } } } 

这在DataGrid中是非常简单的,dg和item类在datagrid中填充,listblock1是一个基本框架。

 private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e) { try { var row_list = (Item)dg.SelectedItem; listblock1.Content = "You Selected: " + row_list.FirstName + " " + row_list.LastName; } catch { } } public class Item { public string FirstName { get; set; } public string LastName { get; set; } } 

刚刚发现这一个,我试过法拉的答案,但它不适用于我的项目。 只需从“数据源”窗口中拖动该列,然后拖放到“标签”或“文本框”。

使用你的Model类来获取从datagrid中select的行值,

  XDocument xmlDoc = XDocument.Load(filepath); if (tablet_DG.SelectedValue == null) { MessageBox.Show("select any record from list..!", "select atleast one record", MessageBoxButton.OKCancel, MessageBoxImage.Warning); } else { try { string tabletID = ""; /*here i have used my model class named as TabletMode*/ var row_list = (TabletModel)tablet_DG.SelectedItem; tabletID= row_list.TabletID; var items = from item in xmlDoc.Descendants("Tablet") where item.Element("TabletID").Value == tabletID select item; foreach (var item in items) { item.SetElementValue("Instance",row_list.Instance); item.SetElementValue("Database",row_list.Database); } xmlDoc.Save(filepath); MessageBox.Show("Details Updated..!" + Environment.NewLine + "TabletId: " +row_list.TabletID + Environment.NewLine + "Instance:" + row_list.Instance + Environment.NewLine + "Database:" + row_list.Database, "", MessageBoxButton.YesNoCancel, MessageBoxImage.Information); } catch (Exception ex) { MessageBox.Show(ex.StackTrace); } } 

如果我select第二行 –

  Dim jason As DataRowView jason = dg1.SelectedItem noteText.Text = jason.Item(0).ToString() 

noteText将是646.这是VB,但你明白了。