向数据网格添加行并从每个单元格中检索新值

时间:2013-12-10 19:00:01

标签: c# wpf datagrid

我需要连续获取每个单元格的每个值。首先,我将数据添加到新行。点击输入后数据消失 - 但是它在我的ObservableCollection中。

我设置了ObservableCollection<object>,以便我可以重复使用Datagrid。例如,我有一个ObservableCollection<object>,其中包含多个CommissionOperator(db / linq对象)对象。在我将新数据插入网格后,我可以说,CommissionOperator中的10个ObservableCollection个对象和object中的ObservableCollection个对象。

现在,我正试图弄清楚如何从Datagrid获取价值。所以我做了一些研究并想出了这个(来自我目前无法找到的链接):

//in a class that inherits from DataGrid
     public Microsoft.Windows.Controls.DataGridCell GetCell( int column )
            {
                Microsoft.Windows.Controls.DataGridRow rowContainer = GetRow(this.SelectedIndex);

                if (rowContainer != null)
                {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>( rowContainer );

                // Try to get the cell but it may possibly be virtualized.
                Microsoft.Windows.Controls.DataGridCell cell = (Microsoft.Windows.Controls.DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex( column );
                if (cell == null)
                {
                    // Now try to bring into view and retreive the cell.
                    this.ScrollIntoView( rowContainer, this.Columns[column] );
                    cell = (Microsoft.Windows.Controls.DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex( column );
                }
                return cell;
            }
            return null;
        }

        public Microsoft.Windows.Controls.DataGridRow GetRow( int _currentRowIndex )
        {
            Microsoft.Windows.Controls.DataGridRow row = (Microsoft.Windows.Controls.DataGridRow)this.ItemContainerGenerator.ContainerFromIndex( _currentRowIndex );
            if (row == null)
            {
                // May be virtualized, bring into view and try again.
                this.UpdateLayout();
                this.ScrollIntoView( this.Items[_currentRowIndex] );
                row = (Microsoft.Windows.Controls.DataGridRow)this.ItemContainerGenerator.ContainerFromIndex( _currentRowIndex );
            }

            return row;
        }

        public static T GetVisualChild<T>( Visual parent ) where T : Visual
        {
            T child = default( T );
            int numVisuals = VisualTreeHelper.GetChildrenCount( parent );
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild( parent, i );
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChild<T>( v );
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }

所以我最终做了这样的事情:

((TextBox) (RGVDataSelector.GetCell(1).Content)).Text

现在我收到错误消息,说我cannot cast TextBlock into a TexBoxTextBlock.Text为空......

我只想弄清楚当我将数据输入新行时如何从DataGrid获取数据......

以下是添加新行后ObservableCollection看起来的样子:

enter image description here

以下是添加期间DataGrid的方式(仅供参考)

enter image description here

以下是插入后的DataGrid

enter image description here

我想,如果我可以从单元格中获取数据,我可以重新绑定DataGrid或更新绑定或其他内容以使其显示正确的数据。

是否会发生这种情况,因为我的DataGrid数据正在消失,实际上我无法从该单元格中获取数据?

0 个答案:

没有答案