C# 如何遍历 WPF 工具包 Datagrid 的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1934529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to loop over the rows of a WPF toolkit Datagrid
提问by Tristan
I have the next code where I defined a WPF toolkit datagrid control called dgQuery; I filled this one with information of a dataset, then I inserted a new checkbox column in dgQuery to check/uncheck some of the rows, I show part of my C# code:
我在下一个代码中定义了一个名为 dgQuery 的 WPF 工具包数据网格控件;我用数据集的信息填充了这个,然后在 dgQuery 中插入了一个新的复选框列来检查/取消选中某些行,我显示了我的 C# 代码的一部分:
dgQuery.DataContext = dS.Tables[0];
DataGridTemplateColumn cbCol = new DataGridTemplateColumn();
cbCol.Header = "Opc";
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(CheckBox));
Binding bind = new Binding("IsSelected");
bind.Mode = BindingMode.TwoWay;
factory.SetValue(CheckBox.IsCheckedProperty, bind);
DataTemplate cellTemplate = new DataTemplate();
cellTemplate.VisualTree = factory;
cbCol.CellTemplate = cellTemplate;
dgQuery.Columns.Insert(0, cbCol);
After checking/unchecking into the new checkbox column of the dgQuery rows I will click a button to save into a database only the rows I checked. The question is, how can I develop the loop for reading all the rows of dgQuery and the condition that will let me know which rows have the checkbox checked/unchecked? Help me with an example, please.
在选中/取消选中 dgQuery 行的新复选框列后,我将单击一个按钮将我选中的行保存到数据库中。问题是,我如何开发循环以读取 dgQuery 的所有行以及让我知道哪些行选中/取消选中复选框的条件?请帮我举个例子。
Thanks!!
谢谢!!
采纳答案by Tony The Lion
this will return a 'row' in your datagrid
这将在您的数据网格中返回一个“行”
public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.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 Microsoft.Windows.Controls.DataGridRow;
if (null != row) yield return row;
}
}
in wpf datagrid, rows are ItemSource.items... no Rows property!
在 wpf 数据网格中,行是 ItemSource.items...没有 Rows 属性!
Hope this helps...
希望这可以帮助...
回答by Tony The Lion
var row = GetDataGridRows(dataGrid1);
/// go through each row in the datagrid
foreach (Microsoft.Windows.Controls.DataGridRow r in row)
{
DataRowView rv = (DataRowView)r.Item;
// Get the state of what's in column 1 of the current row (in my case a string)
string t = rv.Row[1].ToString();
}
回答by Mark Bostleman
Not sure if this is helpful because it assumes a different approach than what you started with, but rather than working directly with the grid, you could bind it to an ObservableCollection of objects that have properties for each column. If you add a bool property in your object for "Selected" and bind the checkbox column to it, you can then query the collection at any time for what is currently selected, like this:
不确定这是否有帮助,因为它采用了与您开始使用的方法不同的方法,但您可以将其绑定到具有每列属性的对象的 ObservableCollection,而不是直接使用网格。如果您在对象中为“Selected”添加 bool 属性并将复选框列绑定到它,则您可以随时查询当前选择的集合,如下所示:
List<MemberEntity> selectedItems =
new List<MemberEntity>(from memberEntity in _memberEntities
where memberEntity.Selected == true
select memberEntity);
//now save selectedItems to the database...
So MemberEntity is a class that has a property for each of the columns in your grid, including a bool called Selected for the checkbox column. _memberEntities is an ObservableCollection of MemberEntity instances. The grid's ItemSource property is bound to _memberEntities and each of its column's Binding properties are bound to a property in MemberEntity like this, assuming that Selected and Name are properties in MemberEntity:
所以 MemberEntity 是一个类,它为网格中的每一列都有一个属性,包括一个名为 Selected 的布尔值,用于复选框列。_memberEntities 是 MemberEntity 实例的 ObservableCollection。网格的 ItemSource 属性绑定到 _memberEntities 并且它的每个列的 Binding 属性都绑定到 MemberEntity 中的一个属性,如下所示,假设 Selected 和 Name 是 MemberEntity 中的属性:
<tk:DataGrid ItemsSource="{Binding _memberEntities}">
<tk:DataGrid.Columns>
<tk:DataGridCheckBoxColumn Binding="{Binding Path=Selected}" />
<tk:DataGridTextColumn Binding="{Binding Path=Name}" />
</tk:DataGrid.Columns>
</tk:DataGrid>
回答by Gayathri Gollavilli
//Looping thought datagrid rows & loop though cells and alert cell values
var row = GetDataGridRows(DataGrid_Standard);
/// go through each row in the datagrid
foreach (Microsoft.Windows.Controls.DataGridRow r in row)
{
DataRowView rv = (DataRowView)r.Item;
foreach (DataGridColumn column in DataGrid_Standard.Columns)
{
if (column.GetCellContent(r) is TextBlock)
{
TextBlock cellContent = column.GetCellContent(r) as TextBlock;
MessageBox.Show(cellContent.Text);
}
else if (column.GetCellContent(r) is CheckBox)
{
CheckBox chk = column.GetCellContent(r) as CheckBox;
MessageBox .Show (chk.IsChecked.ToString());
}
}
}
public IEnumerable<Microsoft.Windows.Controls.DataGridRow> GetDataGridRows(Microsoft.Windows.Controls.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 Microsoft.Windows.Controls.DataGridRow;
if (null != row) yield return row;
}
}