Posted 10 years ago by Chris Chenery
Version: 9.2.0515
Platform: .NET 3.5
Environment: Windows Vista (32-bit)
Avatar
Hi,

I've found two bugs with the Wpf DataGrid.Contrib's new row template. I've downloaded the latest code from codeplex, checked that the problems still exist and made my own fixes. Obviously I don't want to maintain my own version of the DataGrid.Contrib code so thought I'd let you know about the bugs and my solutions.

1. Read-only columns and the new row template don't work well together. OnDataGridRowMouseLeftButtonDown doesn't check that the cell can be edited before giving it focus (only really an issue if the very first column is read-only and you want to auto-fill values when a new row is created). Changed code:
        private static void OnDataGridRowMouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
            DataGridRow row = sender as DataGridRow;
            if (null == row)
                return;

            DataGridControl datagrid = VisualTreeHelperExtended.GetAncestor(row, typeof(DataGridControl)) as DataGridControl;
            if (null == datagrid)
                return;

            if (CollectionView.NewItemPlaceholder == row.Item) {
                ControlTemplate template = GetTemplate(datagrid);
                if (row.Template == template) {
                    row.Template = GetDefaultTemplate(datagrid);
                    row.UpdateLayout();

                    datagrid.CurrentItem = row.Item;
                    DataGridColumn column = datagrid.Columns.FirstOrDefault(col => !col.IsReadOnly);

                    if (column != null) {
                        DataGridCell cell = VisualTreeHelperExtended.GetAncestor(column.GetCellContent(row), typeof(DataGridCell)) as DataGridCell;

                        if (cell != null)
                            cell.Focus();
                    }

                    datagrid.BeginEdit();
                }
            }
        }
2. Setting CanUserAddRows to false and then adding rows using IEditableCollectionView.AddNew results in a crash. I just modified DataGridExtensions.cs to check for invalid indices. Changed code:
        public static DataGridRow GetRow(this DataGridControl dataGrid, object item) {
            int index = dataGrid.Items.IndexOf(item);
            return ((index != -1) ? dataGrid.GetRow(index) : null);
        }
Cheers

Chris

Comments (1)

Posted 10 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Chris,

Thanks for sharing your code, which we've integrated for the 2010.1 release.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.