WPF DataGrid c# 获取当前行的数量 获取指定索引行指定索引列的单元格的值
时间: 2024-09-29 12:07:05 浏览: 206
在WPF(Windows Presentation Foundation)中,DataGrid是一个用于显示和编辑数据的控件。如果你想获取当前行的数量以及指定行和列的单元格值,可以按照以下步骤操作:
1. **获取当前行数**:
使用`Items.Count`属性可以获取DataGrid内所有行的数据项总数。例如:
```csharp
int numRows = dataGrid.Items.Count;
```
2. **获取指定行指定列的单元格值**:
首先,你需要确定每一行都是DataGrid的一个`DataItem`,然后你可以通过`DataGrid.SelectedItem`属性获取选中的行,如果需要特定索引的行,可以用`Items[index]`代替。对于获取特定单元格的值,你可以使用`DataGrid.Columns`集合中的`DataGridTextColumn`(或其他类型的`DataGridColumn`),结合`DataGrid.SelectedItem`来访问。
```csharp
int rowIndex = 0; // 你想查询的行索引
string cellValue = (string)dataGrid.Columns["YourColumnName"].BindingExpression.Value;
if (rowIndex < numRows)
{
var item = dataGrid.Items[rowIndex];
cellValue = ((YourDataType)item).YourProperty; // 假设你的列绑定了`YourProperty`属性
}
```
这里,“YourColumnName”是你要查询的列的头名,“YourDataType”是该列对应的`DataItemType`,而“YourProperty”则是你想要获取的列的实际属性名。
阅读全文
相关推荐


















