c# - DataGrid 列中的按钮,获取它来自 Click 事件处理程序的行的单元格值

标签 c# wpf xaml datagrid

xml:我的数据网格中的按钮

<DataGridTemplateColumn Header="Update">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Content="Update" Name="btnEmpGridUpdate" Tag="{Binding Path = EMPID}" Click="btnEmpGridUpdate_Click" ></Button>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

按钮事件中的代码:

Object Id = new Object();
Id =  ((Button)sender).Tag;

这是检索选定行的所有单元格值的可能方法吗?如果,我该怎么做?否则,请建议我使用其他方法从数据网格更新我的数据集。提前致谢 。 :)

最佳答案

使用这些辅助函数; http://techiethings.blogspot.com/2010/05/get-wpf-datagrid-row-and-cell.html

在您的更新按钮上,连接 Click 事件,并在处理程序中执行如下操作:

//youll need to google for GetVisualParent function.
DataGridRow row = GetVisualParent<DataGridRow>(button);
for(int j = 0; j < dataGrid.Columns.Count; j++)
   DataGridCell cell = GetCell(dataGrid, row, j);

我不完全明白你想做什么,但这应该有所帮助。

关于c# - DataGrid 列中的按钮,获取它来自 Click 事件处理程序的行的单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11601198/

相关文章:

c# - 在 C# 中将 MySQL 数据库嵌入到应用程序中

c# - 改变进度条的大小

c# - WPF 使用 € 符号将字符串格式化为货币

c# - SQL Server 日志文件使用 Hangfire 增长了 40GB

C# Azure 获取包含文件的特定目录

c# - NullReferenceException 在 WPF 中为 vtk 加载 RenderWindowControl

.net - WPF的标签云控制

c# - 动态 WPF 图像加载问题

c# - 当使用数据表作为 IListSource 填充时,根据值更改 WPF DataGrid 单元格的背景

c# - 来自 Excel.Worksheet.Cells[行,列] 的奇怪行为