如何在wpf中获取datagrid的行和单元格 [英] how to get rows and cells of datagrid in wpf
本文介绍了如何在wpf中获取datagrid的行和单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨专家
我想获取 WPF中 dataGrid 中每一行的每个单元格的值按钮点击事件。按钮在DataGrid之外
plz help
谢谢
解决方案
在你的事件处理程序中使用它
var rows = GetDataGridRows(datagrid1);
foreach (DataGridRow r 行)
{
// DataRowView rv =(DataRowView)r.Item;
foreach (DataGridColumn列 in datagrid1.Columns)
{
if (column.GetCellContent(r) TextBlock)
{
TextBlock cellContent = column.GetCellContent(r)< span class =code-keyword> as TextBlock;
MessageBox.Show(cellContent.Text);
}
}
}
从数据网格获取行的方法
< pre lang =c#> public IEnumerable< datagridrow> GetDataGridRows(DataGrid网格)
{
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 返回行;
}
}
< / datagridrow >
这是一个软件错误:
将列可见性更改为可见。
删除DataGridTemplateColumn。
它将正常工作。
Hi Experts
I want to get values of each cell for every row in dataGrid in WPF On a button click event .Button is Outside of the DataGrid
plz help
Thanks
解决方案
Use this in youer event handler
var rows = GetDataGridRows(datagrid1); foreach (DataGridRow r in rows) { // DataRowView rv = (DataRowView)r.Item; foreach (DataGridColumn column in datagrid1.Columns) { if (column.GetCellContent(r) is TextBlock) { TextBlock cellContent = column.GetCellContent(r) as TextBlock; MessageBox.Show(cellContent.Text); } } }
Method to get the rows from datagrid
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; } } </datagridrow>
It is a software bug so :
change column visibility to visible.
remove DataGridTemplateColumn.
it will work good.
这篇关于如何在wpf中获取datagrid的行和单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文