I have a datagrid and textboxes. When I select a row in DataGrid I want the textboxes to be filled with related data of that datagrid column. Here is the picture.,
Here's how you could do it with bindings. The "Text" parameter of each textbox should be modified thusly:
Text = "{Binding ElementName = personelEntityDataGrid, Path = CurrentItem.<FIELD_NAME_HERE>}"
so for the nameBox, you would have:
Text = "{Binding ElementName = personelEntityDataGrid, Path = CurrentItem.Name}"
Don't check in Loaded event, instead Check in selectionChanged event, also add a null check to your pers object
private void personelEntityDataGrid_SelectionChanged(object sender, RoutedEventArgs e)
{
PersonelEntity pers = (PersonelEntity)personelEntityDataGrid.SelectedItem;
if(pers != null)
{
NameBox.Text = pers.Name; // I get exception here
AgeBox.Text = pers.Age.ToString();
PhoneNumberBox.Text = pers.PhoneNumber;
AddresBox.Text = pers.Address;
}
}