获取DataGrid WPF中的选定行项目
我有一个DataGrid,绑定到数据库表,我需要在DataGrid中获取所选行的内容,例如,我想在MessageBox中显示所选行的内容 DataGrid的示例:获取DataGrid WPF中的选定行项目,wpf,datagrid,selecteditem,Wpf,Datagrid,Selecteditem,我有一个DataGrid,绑定到数据库表,我需要在DataGrid中获取所选行的内容,例如,我想在MessageBox中显示所选行的内容 DataGrid的示例: 因此,如果我选择第二行,我的消息框必须显示如下内容:646 Jim Biology。如果您使用MVVM模式,您可以将VM的SelectedRecord属性与DataGrid的SelectedItem绑定,这样您的VM中始终具有SelectedValue。 否则,您应该使用DataGrid的SelectedIndex属性。您可以使用Se
因此,如果我选择第二行,我的消息框必须显示如下内容:646 Jim Biology。如果您使用MVVM模式,您可以将VM的SelectedRecord属性与DataGrid的SelectedItem绑定,这样您的VM中始终具有SelectedValue。 否则,您应该使用DataGrid的SelectedIndex属性。您可以使用SelectedItem属性获取当前选定的对象,然后将其转换为正确的类型。例如,如果您的DataGrid绑定到一组客户对象,则可以执行以下操作: Customer=CustomermyDataGrid.SelectedItem; 或者,您可以将SelectedItem绑定到源类或ViewModel
好吧,我将提出类似的解决方案,这对我来说很好
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)
{
}
}
如果我选择第二行-
Dim jason As DataRowView
jason = dg1.SelectedItem
noteText.Text = jason.Item(0).ToString()
注:文本为646。这是VB,但你明白了。这在DataGrid dg中非常简单,item类填充在DataGrid中,listblock1是一个基本框架
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
{
}
}
}
}
}
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; }
}
在我尝试法拉的答案后发现了这个,但它在我的项目中不起作用。只需从“数据源”窗口拖动该列,并将其放到标签或文本框中。使用模型类从datagrid中选择行值,如
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);
}
}
您还可以:
DataRowView row = dataGrid.SelectedItem as DataRowView;
MessageBox.Show(row.Row.ItemArray[1].ToString());
@用MVVM回答Krytox问题
<DataGrid
Grid.Column="1"
Grid.Row="1"
Margin="10" Grid.RowSpan="2"
ItemsSource="{Binding Data_Table}"
SelectedItem="{Binding Select_Request, Mode=TwoWay}" SelectionChanged="DataGrid_SelectionChanged"/>//The binding
#region View Model
private DataRowView select_request;
public DataRowView Select_Request
{
get { return select_request; }
set
{
select_request = value;
OnPropertyChanged("Select_Request"); //INotifyPropertyChange
OnSelect_RequestChange();//do stuff
}
}
这里有很多答案可能在特定的上下文中起作用,但我只是尝试获取选定行中第一个单元格的文本值。虽然这里接受的答案对我来说是最接近的,但它仍然需要创建一个类型并将行强制转换为该类型。我在寻找一个更简单的解决方案,这就是我想到的: MessageBox.ShowDataRowViewDataGrid.SelectedItem.Row[0]。ToString
这将给出所选行中的第一列。希望这能帮助其他人。我不使用MVVM,我只是从WPF/C/.NET开始。如果我写«ContentDataGrid.SelectedIndex»,我会得到DataGrid中所选行的索引,我不需要索引,而是实际值,比如«646»。那么,我怎么才能得到它呢?你应该考虑使用绑定的对象,这样你就可以绑定DATAGRID的SelectedItem属性。在您的情况下,您应该尝试导航到Datagrid属性,以确定它是否存储所选项目属性。+1。第一句话正是我想要的!SelectedRecord属性?我想我必须自己设置。任何建议或代码示例都可以great@ema,如果您能提供代码示例,那就太好了。第二个示例正是我想要的。谢谢如何从给定的替代方法获取数据?第二种方法非常有效,需要补充的一点可能对像我这样刚开始使用wpf的人有所帮助。在这个问题中,我们关注的是客户类。如果您没有为行定义类,则RowView作为System.data中的数据类型工作。当然,这是一个品味问题,但我个人更喜欢公认的答案,因为它基于标准API,更易于阅读Customer=CustomermyDataGrid.SelectedItem;-一行解决方案,清晰明了。不,没有索引行[0],没有ToString-太多额外的操作,无法通过一个命令SelectedItem获得API提供的内容。这是假设您有一个Customer类。如果您的DataGrid是从数据库查询填充的,但是您没有DataGrid中的行的特定类,那么接受的答案将无法工作,除非我遗漏了什么。对于我来说,也许更好的解决方案是创建这样一个类,但是对于我自己不认为可以重用的东西,这似乎是多余的。需要什么额外的代码来实现这一点?从设计的角度来看,不需要额外的代码,但显然WPF设计得不好
<DataGrid
Grid.Column="1"
Grid.Row="1"
Margin="10" Grid.RowSpan="2"
ItemsSource="{Binding Data_Table}"
SelectedItem="{Binding Select_Request, Mode=TwoWay}" SelectionChanged="DataGrid_SelectionChanged"/>//The binding
#region View Model
private DataRowView select_request;
public DataRowView Select_Request
{
get { return select_request; }
set
{
select_request = value;
OnPropertyChanged("Select_Request"); //INotifyPropertyChange
OnSelect_RequestChange();//do stuff
}
}