浏览代码

Fixed NPE when changing columns while editing a grid row.

Kenric Nugteren 1 年之前
父节点
当前提交
c2c3a91dc7
共有 1 个文件被更改,包括 24 次插入7 次删除
  1. 24 7
      inabox.wpf/DynamicGrid/DynamicGrid.cs

+ 24 - 7
inabox.wpf/DynamicGrid/DynamicGrid.cs

@@ -389,7 +389,10 @@ namespace InABox.DynamicGrid
 
         private readonly DynamicRowMovementColumn? up;
 
-        private DataTable DataGridItems => (DataGrid.ItemsSource as DataTable)!;
+        /// <summary>
+        /// <see langword="null"/> when <see cref="DataGrid.ItemsSource"/> is <see langword="null"/>, generally while the grid is refreshing its columns.
+        /// </summary>
+        private DataTable? DataGridItems => (DataGrid.ItemsSource as DataTable);
 
         #region Events
 
@@ -873,7 +876,7 @@ namespace InABox.DynamicGrid
         private CoreRow? GetRowFromIndex(int rowIndex)
         {
             var row = rowIndex - (Options.Contains(DynamicGridOption.FilterRows) ? 2 : 1);
-            if (row < 0)
+            if (row < 0 || DataGridItems is null)
                 return null;
             row = DataGridItems.Rows.IndexOf((DataGrid.View.Records[row].Data as DataRowView)!.Row);
             if (row < 0)
@@ -941,10 +944,10 @@ namespace InABox.DynamicGrid
         private void DataGrid_CurrentCellBeginEdit(object? sender, CurrentCellBeginEditEventArgs e)
         {
             var headerrows = Options.Contains(DynamicGridOption.FilterRows) ? 2 : 1;
-            if (e.RowColumnIndex.RowIndex < headerrows)
+            if (e.RowColumnIndex.RowIndex < headerrows || DataGridItems is null)
                 return;
-            if (inplaceeditor == null)
-                inplaceeditor = LoadItem(Data.Rows[e.RowColumnIndex.RowIndex - headerrows]);
+
+            inplaceeditor ??= LoadItem(Data.Rows[e.RowColumnIndex.RowIndex - headerrows]);
 
             var column = DataGrid.Columns[e.RowColumnIndex.ColumnIndex] as GridComboBoxColumn;
             if (column != null && column.ItemsSource == null)
@@ -997,6 +1000,8 @@ namespace InABox.DynamicGrid
         {
             var datacolname = colname.Replace(".", "_");
             var table = DataGridItems;
+            if (table is null) return;
+
             var colno = table.Columns.IndexOf(datacolname);
             var corecol = Data.Columns[colno].ColumnName;
             var corerow = Data.Rows[row];
@@ -1014,7 +1019,7 @@ namespace InABox.DynamicGrid
             if (inplaceeditor is not null && bChanged) UpdateData(inplaceeditor, e.RowColumnIndex.ColumnIndex);
             bChanged = false;
             inplaceeditor = null;
-            DataGridItems.AcceptChanges();
+            DataGridItems?.AcceptChanges();
         }
 
         private void UpdateData(T obj, int columnindex)
@@ -1022,7 +1027,7 @@ namespace InABox.DynamicGrid
             var table = DataGridItems;
 
             var iRow = SelectedRows.First().Index; //e.RowColumnIndex.RowIndex - (Options.Contains(DynamicGridOptions.FilterRows) ? 2 : 1);
-            if (iRow > table.Rows.Count)
+            if (table is null || iRow > table.Rows.Count)
                 return;
             var row = table.Rows[iRow];
             var colname = DataGrid.Columns[columnindex].MappingName;
@@ -1104,6 +1109,8 @@ namespace InABox.DynamicGrid
             var result = new List<CoreRow>();
 
             var table = DataGridItems;
+            if (table is null) return Array.Empty<CoreRow>();
+
             var rows = DataGrid.View.Records.Select(x => (x.Data as DataRowView)!).ToList();
             foreach (var row in rows)
             {
@@ -2195,6 +2202,12 @@ namespace InABox.DynamicGrid
 
         public void InvalidateRow(CoreRow row)
         {
+            var table = DataGridItems;
+            if(table is null)
+            {
+                return;
+            }
+
             var rowdata = new List<object?>(row.Values);
             foreach (var ac in ActionColumns)
                 rowdata.Add(ac.Data(row));
@@ -2262,6 +2275,10 @@ namespace InABox.DynamicGrid
         {
             var result = new List<CoreRow>();
             var table = DataGridItems;
+            if(table is null)
+            {
+                return Array.Empty<CoreRow>();
+            }
             var rows = DataGrid.View.Records.Select(x => (x.Data as DataRowView)!).ToList();
             foreach (var row in rows)
             {