首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从DataRowView获取DataGridRow?

如何从DataRowView获取DataGridRow?
EN

Stack Overflow用户
提问于 2016-09-02 06:08:40
回答 2查看 1.1K关注 0票数 0

我已经创建了一个用SqlClient DataTables中的数据加载2 DataGrids的WPF应用程序。我有下面的方法来比较这两个表。它工作得很好,但我一定是在破坏它的代码中做了一些改动。现在,dataGridRow1和dataGridRow2变量始终为空。如何从DataRowView获取DataGridRow?

代码语言:javascript
复制
    private void CompareDataTables(string primaryKey)
    {
        foreach (DataRowView dataRowView1 in DataGrid1.ItemsSource)
        {
            DataGridRow dataGridRow1 = DataGrid1.ItemContainerGenerator.ContainerFromItem(dataRowView1) as DataGridRow;
            if (dataGridRow1 != null)
            {
                int primaryKeyValue1 = (int)dataRowView1.Row[primaryKey];
                foreach (DataRowView dataRowView2 in DataGrid2.ItemsSource)
                {
                    //DataRowView dataRowView2 = (DataRowView)dataGridRow2.Item;
                    int primaryKeyValue2 = (int)dataRowView2.Row[primaryKey];
                    if (primaryKeyValue1 == primaryKeyValue2)
                    {
                        foreach (DataGridColumn column in DataGrid1.Columns)
                        {
                            DataGridRow dataGridRow2 = DataGrid2.ItemContainerGenerator.ContainerFromItem(dataRowView2) as DataGridRow;
                            if (dataGridRow2 != null)
                            {
                                FrameworkElement frameworkElement1 = column.GetCellContent(dataGridRow1);
                                FrameworkElement frameworkElement2 = GetFrameworkElement(DataGrid2, dataGridRow2, (String)column.Header);
                                if (frameworkElement1 is TextBlock && frameworkElement2 is TextBlock)
                                {
                                    TextBlock textBlock1 = frameworkElement1 as TextBlock;
                                    TextBlock textBlock2 = frameworkElement2 as TextBlock;
                                    if (textBlock1.Text != textBlock2.Text)
                                    {
                                        textBlock1.Background = Brushes.LightSalmon;
                                        textBlock2.Background = Brushes.LightSalmon;
                                    }
                                }
                            }
                        }
                        break;
                    }
                }
            }
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2016-09-02 16:39:13

请使用grid.ItemContainerGenerator.ContainerFromIndex函数代替grid.ItemContainerGenerator.ContainerFromItem,,因为Datadrid使用的是虚拟化,所以当时不会生成项目。

票数 0
EN

Stack Overflow用户

发布于 2016-09-02 21:48:00

这似乎解决了问题。将下面这行代码放在ContinerFromItem之前:

DataGrid1.UpdateLayout();

谁能猜到呢?这是另一个问题的链接,我在那里找到了这个答案:

ItemContainerGenerator.ContainerFromItem() returns null while VirtualizingStackPanel.IsVirtualizing="False"

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39281619

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档