I have a DataGrid
that has a selection of Jobs
that users can add/update/delete. My problem with my DataGrid
is that when the data is loaded into it, I want it to automatically fill in some TextBoxes
with details of the first item in the DataGrid
. I do this by writing the method for the SelectionChanged
event on the DataGrid
;
private void DataGridSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedJob = dataGrid.SelectedItem as JobModel;
//MessageBox.Show(selectedJob.ITName);
if (selectedJob != null)
{
notesTextBox.Text = selectedJob.CaseNotes;
createdPicker.SelectedDate = DateTime.Parse(selectedJob.DateCreated.ToString("yyyy-MM-dd"));
deadlinePicker.SelectedDate = DateTime.Parse(selectedJob.DateDeadline.ToString("yyyy-MM-dd"));
nameTextBox.Text = selectedJob.CaseClient;
itNameComboBox.SelectedItem = selectedJob.ITName;
}
}
The issue here is that the TextBoxes
(and DatePickers
) are not filled in when the DataGrid
loads. However, if I uncomment the MessageBox
then the data is filled in correctly. This is a big problem as if there is only one job in the DataGrid
then the user cannot select it to delete or update it. Here is the XAML
for the DataGrid
itself;
<DataGrid x:Name="dataGrid" ColumnWidth="*" IsReadOnly="true"
Margin="10" Grid.ColumnSpan="3" FontSize="18.667"
HeadersVisibility="Column" AutoGenerateColumns="False"
CanUserAddRows="False"
IsSynchronizedWithCurrentItem="True"
EnableRowVirtualization="True"
ItemsSource="{Binding JobsCollectionView}"
MinColumnWidth="0" SelectionChanged="DataGridSelectionChanged">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Overdue}" Value="1">
<Setter Property="Background" Value="Red"/>
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<!--<DataGridTextColumn Header="Case NO" Width="*" Binding="{Binding CaseNumber}"/>!-->
<DataGridTextColumn Header="IT Person" Width="*" Binding="{Binding ITName}"/>
<DataGridTextColumn Header="Case Notes" Width="*" Binding="{Binding CaseNotes}"/>
<DataGridTextColumn Header="Date Created" Width="*" Binding="{Binding DateCreated, StringFormat=dd/MM/yyyy}"/>
<DataGridTextColumn Header="Deadline Date" Width="*" Binding="{Binding DateDeadline, StringFormat=dd/MM/yyyy}"/>
<DataGridTextColumn Header="Employee Name" Width="*" Binding="{Binding CaseClient}"/>
</DataGrid.Columns>
</DataGrid>
And the method that fills the DataGrid
;
public void FillDataGrid()
{
_jobDataService = new JobDataService();
var _jobs = new ObservableCollection<JobModel>();
_jobs = _jobDataService.GetJobList();
JobsCollectionView = CollectionViewSource.GetDefaultView(_jobs);
DataContext = this;
foreach (JobModel currentJob in JobsCollectionView)
{
if (DateTime.Now == currentJob.DateDeadline)
{
currentJob.Overdue = 0;
}
else if (DateTime.Now > currentJob.DateDeadline)
{
currentJob.Overdue = 1;
}
}
}
Is there something obvious I am missing that is preventing the SelectionChanged
event from being called? I find it strange how if I uncomment the MessageBox
it works as I intended.