I am trying to bind to a DataTable
to a DataGrid
in wpf.
Here is my code:
My Model:
private DataTable _runNumbers;
public DataTable RunNumbers
{
set
{
_runNumbers = value;
}
get
{
if (_runNumbers == null) { _runNumbers = new DataTable("RunNumbers"); }
return _runNumbers;
}
}
public RunNumberModel() //Constructor
{
RunNumbers.Columns.Add(" ", typeof(string));
RunNumbers.Columns[" "].ReadOnly = true;
RunNumbers.Rows.Add("Test");
}
My view:
<DataGrid Grid.Row="1" Grid.Column="1" Width="400" HorizontalAlignment="Center" Margin="20" ItemsSource="{Binding RunNumbers.RunNumbers}" />
I cannot seem to figure out how to show the value of the cell rather then System.Data.DataRowView
- I was thinking perhaps I need a DataTemplate
of some sort but I am not sure how I would set this up.
I have looked at many other posts online and have had no success.
If anyone knows the problem I would really appreciate it. Thanks!
It should work if you use a valid column name like for example "x":
public RunNumberModel() //Constructor
{
RunNumbers.Columns.Add("x", typeof(string));
RunNumbers.Columns["x"].ReadOnly = true;
RunNumbers.Rows.Add("Test");
}