Learning WPF, coming from WinForm experience. I’m working on converting a WinForm program that uses a DataGridView to display (text) in multiple columns with project search results. The DataGridView is 5 columns 4 of which is text and the first column is integer.
I have created in the new WPF project a DataGridView mimicking my WinForm project as best as I can reason
<DataGrid x:Name="dataGrid" Grid.Row="3" Grid.Column="5" AutoGenerateColumns="False" IsEnabled="{Binding IsdataGrid}"
ItemsSource="{Binding GridDataTable}">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding gvID}" Width="30"/>
<DataGridTextColumn Header="File Name" Binding="{Binding gvFileName}" Width="240"/>
<DataGridTextColumn Header="Number of Lines" Binding="{Binding gvNumberOfLines}" Width="80"
HeaderStyle="{StaticResource WrappedColumnHeaderStyle}"/>
<DataGridTextColumn Header="Number of Matches" Binding="{Binding gvNumberOfMatches}" Width="80"
HeaderStyle="{StaticResource WrappedColumnHeaderStyle}"/>
<DataGridTextColumn Header="Duration" Binding="{Binding gvDuration}" Width="60"/>
</DataGrid.Columns>
I’m invoking the grid population from a button press (AsyncRelayCommand(OnbtnProcessExecute, () => true);) not a button_click method.
My public class of
public class DataGridInput
{
public int ID { get; set; }
public string gvFileName { get; set; }
public string gvNumberOfLines { get; set; }
public string gvNumberOfMatches { get; set; }
public string gvDuration { get; set; }
}
My grid declaration in the MainwindowsViewModel
private DataGrid _gridDataTable;
public DataGrid GridDataTable
{
get => _gridDataTable;
set
{
if(_gridDataTable == value)
return;
_gridDataTable = value;
OnPropertyChanged(nameof(GridDataTable));
}
}
When the body of the project finds details of a file before processing to the next files I would like to populate a row in DataGridView with the column data like:
Id, Filename, Number of lines, Number of lines, duration
All are in the processing result. In WinForm’s DataGridView it was a matter of doing:
dataGridView1.Rows.Add(result[0], result[1], result[2], result[3]), result[4];
I have search far and near but can’t find information that I can use to help me write to the DataGridView. I have tried
List<DataGridInput> dgi = new List<DataGridInput>();
dgi.Add(new DataGridInput()
{
ID = 1,
gvFileName = "Testthis.txt",
gvNumberOfLines = "20",
gvNumberOfMatches = "22",
gvDuration = "00:03:00"
});
GridDataTable.ItemsSource = dgi;
But I’m getting “System.NullReferenceException Message=Object reference not set to an instance of an object.”
In short I’m over my head, looking for assistance on how to convert my WinForm.DataGridView to a working WPF model.