在编辑时使用 DataTable 绑定数据网格 System.NullReferenceException:'Object reference not set to an instance of an object.'
Datagrid binding using DataTable on edit System.NullReferenceException: 'Object reference not set to an instance of an object.'
On Edit of datagrid throws null exception,对于使用datatable绑定的datagrid,填充datatable的代码如下xaml.cs。
数据表绑定正在编辑表示框架中的单元格 Null 异常,使用的框架是 .NET 4.6
//DataTable created in xaml.cs,
private DataTable _dataTable3cross3 = new DataTable("table3cross3");
public DataTable DataTable3cross3
{
get { return _dataTable3cross3; }
set
{
_dataTable3cross3 = value;
OnPropertyChanged("DataTable3cross3");
}
}
//Xaml
<DataGrid x:Name="grid1" Margin="10" Grid.Column="0" Grid.Row="2"
ItemsSource="{Binding}" BorderThickness="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
HeadersVisibility="Column" GridLinesVisibility="All"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserSortColumns = "False" CanUserReorderColumns="False"
CanUserResizeColumns="False" CanUserResizeRows="False"
RowHeight="36" AutoGenerateColumns="True" AutoGeneratingColumn="r2_AutoGeneratingColumn" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
//xaml.cs
private void CreateTable()
{
try
{
//Clearing the columns to update the with new values
_dataTable3cross3.Clear();
_dataTable3cross3.Columns.Clear();
// Adding columns
for (int i = 0; i <= 3; i++)
{
if (i == 0)
{
this._dataTable3cross3.Columns.Add("Ranges Values");
}
else
{
this._dataTable3cross3.Columns.Add("T" + i.ToString() + " " + "[c]");
}
}
//Rows added from list
if (TempRangeGasValues3x3.Count > 0)
{
this._dataTable3cross3.Rows.Add(12, 13, 14, 15);
}
if (PressRangeGasValues3x3.Count > 0 && CompZValues3x3.Count > 0)
{
this._dataTable3cross3.Rows.Add(1, 2, 3, 4);
this._dataTable3cross3.Rows.Add(5, 6, 7, 8);
this._dataTable3cross3.Rows.Add(9, 6, 3, 4);
}
grid1.DataContext = DataTable3cross3;
}
catch (Exception ex)
{
}
}
//[下面是数据网格编辑的异常][1]
//[1]: https://i.stack.imgur.com/uJt2w.png
删除其他 space 或列中的字符串将解决问题
这个。_dataTable3cross3.Columns.Add("T" + i.ToString());
或者
将“[]”大括号更改为“(c)”也可以解决问题。
On Edit of datagrid throws null exception,对于使用datatable绑定的datagrid,填充datatable的代码如下xaml.cs。 数据表绑定正在编辑表示框架中的单元格 Null 异常,使用的框架是 .NET 4.6
//DataTable created in xaml.cs,
private DataTable _dataTable3cross3 = new DataTable("table3cross3");
public DataTable DataTable3cross3
{
get { return _dataTable3cross3; }
set
{
_dataTable3cross3 = value;
OnPropertyChanged("DataTable3cross3");
}
}
//Xaml
<DataGrid x:Name="grid1" Margin="10" Grid.Column="0" Grid.Row="2"
ItemsSource="{Binding}" BorderThickness="2" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
HeadersVisibility="Column" GridLinesVisibility="All"
CanUserAddRows="False" CanUserDeleteRows="False"
CanUserSortColumns = "False" CanUserReorderColumns="False"
CanUserResizeColumns="False" CanUserResizeRows="False"
RowHeight="36" AutoGenerateColumns="True" AutoGeneratingColumn="r2_AutoGeneratingColumn" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" />
//xaml.cs
private void CreateTable()
{
try
{
//Clearing the columns to update the with new values
_dataTable3cross3.Clear();
_dataTable3cross3.Columns.Clear();
// Adding columns
for (int i = 0; i <= 3; i++)
{
if (i == 0)
{
this._dataTable3cross3.Columns.Add("Ranges Values");
}
else
{
this._dataTable3cross3.Columns.Add("T" + i.ToString() + " " + "[c]");
}
}
//Rows added from list
if (TempRangeGasValues3x3.Count > 0)
{
this._dataTable3cross3.Rows.Add(12, 13, 14, 15);
}
if (PressRangeGasValues3x3.Count > 0 && CompZValues3x3.Count > 0)
{
this._dataTable3cross3.Rows.Add(1, 2, 3, 4);
this._dataTable3cross3.Rows.Add(5, 6, 7, 8);
this._dataTable3cross3.Rows.Add(9, 6, 3, 4);
}
grid1.DataContext = DataTable3cross3;
}
catch (Exception ex)
{
}
}
//[下面是数据网格编辑的异常][1]
//[1]: https://i.stack.imgur.com/uJt2w.png
删除其他 space 或列中的字符串将解决问题
这个。_dataTable3cross3.Columns.Add("T" + i.ToString());
或者 将“[]”大括号更改为“(c)”也可以解决问题。