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

因此,如果我选择第二行,我的MessageBox必须显示如下内容:646Jim Biology。
发布于 2011-04-20 06:01:10
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 { }
}https://stackoverflow.com/questions/3913580
复制相似问题