When using DataTable and DataGrid in WPF System.Windows.Data Error Occurs

I am using DataGrid WPFToolkit to display some data.

DataTable is initialized in myfile.xaml.cs using

myTable = new DataTable();
DataColumn col;
col = new DataColumn();
col.DataType = System.Type.GetType("System.Int64");
col.ColumnName = "ID";
col.ReadOnly = true;
col.Unique = false;
myTable.Columns.Add(col);

col = new DataColumn();
col.DataType = System.Type.GetType("System.String");
col.ColumnName = "Name";
col.ReadOnly = true;
col.Unique = false;
myTable.Columns.Add(col);

etc.

As suggested here, I use

myGrid.ItemsSource = myTable.DefaultView;

in myfile.xaml.cs file.

In myfile.xaml, I defined only

<my:DataGrid Name="myGrid" xmlns:my="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"/>

When I add a record to a table with

DataRow row = myTable.NewRow();
row["ID"] = 123;
row["Name"] = "MyName";

Action action = () => myTable.Rows.Add(row);
Dispatcher.Invoke(action);

the record is added correctly to the grid in the GUI, however I get the following error:

System.Windows.Data Error: 39 : BindingExpression path error: 'ID' property not found on 'object' ''Object' (HashCode=29890231)'. BindingExpression:Path=ID; DataItem='Object' (HashCode=29890231); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 39 : BindingExpression path error: 'Name' property not found on 'object' ''Object' (HashCode=29890231)'. BindingExpression:Path=Name; DataItem='Object' (HashCode=29890231); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

So how can I fix this error? Any clues?

Thank.

+3
source share
1 answer

, - , . :

DataRow row = myTable.NewRow();
row["ID"] = 123;
row["Name"] = "MyName";

Action action = () => myTable.Rows.Add(row);
Dispatcher.Invoke(action);

:

DataRow row = myTable.NewRow();
myTable.Rows.Add(row);
row["ID"] = 123;
row["Name"] = "MyName";

.: dataTable

0

Source: https://habr.com/ru/post/1775659/


All Articles