I have a big problem to get the values from datagrid selected row to the textboxes.
The user should select one row and the view should get the values from the row in the specific textbox. After the user can make changes inside the textbox and update the database. After several hours on Youtube tutorials and Google i still dont have any idear how i can get to the point (Values from selected row in textbox). Please help me
Below is the Code for View and ViewModel
UserControl:
<!--Database datagrid-->
<materialDesign:Card Margin="5">
<DataGrid x:Name="MachineDataGrid" AutoGenerateColumns="False" MaxHeight="750" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" ItemsSource="{Binding DataContext}">
<DataGrid.Columns>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgMachineID}" Binding="{Binding MachineID, Mode=TwoWay}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgCustomerId}" Binding="{Binding CustomerID}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgCustomerName}" Binding="{Binding CustomerName}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgCity}" Binding="{Binding City}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgCountry}" Binding="{Binding Country}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgSpindleC1}" Binding="{Binding SpindleC1}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgSpindleC2}" Binding="{Binding SpindleC2}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgHoningHead}" Binding="{Binding HoningHead}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgNCVersion}" Binding="{Binding NCVersion}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgHMIVersion}" Binding="{Binding HMIVersion}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgHRIVersion}" Binding="{Binding HRIVersion}"/>
<materialDesign:DataGridTextColumn Header="{x:Static language:Lang.DgAHSVersion}" Binding="{Binding AHSVersion}"/>
</DataGrid.Columns>
</DataGrid>
</materialDesign:Card>
ViewModel:
public ICommand SaveCommand { get; private set; }
public ICommand UpdateCommand { get; private set; }
public ICommand DeleteCommand { get; private set; }
public ICommand PrintCommand { get; private set; }
public ICommand ShowAdvCommand { get; private set; }
public ObservableCollection<Machine> DataContext { get; set; }
public Machine MachineSelectedItem { get; set; }
public object MachineDataGrid { get; private set; }
private PRWContext context = new PRWContext();
public MachineViewModel()
{
SaveCommand = new RelayCommand(() => ExecuteSaveCommand());
UpdateCommand = new RelayCommand(() => ExecuteUpdateCommand());
DeleteCommand = new RelayCommand(() => ExecuteDeleteCommand());
PrintCommand = new RelayCommand(() => ExecutePrintCommand());
LoadData();
}
private void ExecuteSaveCommand()
{
Machine machine = new Machine
{
MachineID = MachineID,
CustomerID = CustomerID,
CustomerName = CustomerName,
City = City,
Country = Country,
SpindleC1 = SpindleC1,
SpindleC2 = SpindleC2,
HoningHead = HoningHead,
NCVersion = NCVersion,
HMIVersion = HMIVersion,
HRIVersion = HRIVersion,
AHSVersion = AHSVersion
};
context.Machines.Add(machine);
context.SaveChanges();
ClearText();
}
private void ExecuteUpdateCommand()
{
Machine machine = context.Machines.FirstOrDefault(w => w.MachineID == MachineID);
machine.CustomerID = CustomerID;
machine.CustomerName = CustomerName;
context.SaveChanges();
ClearText();
}
private void ExecuteDeleteCommand()
{
throw new NotImplementedException();
}
private void ExecutePrintCommand()
{
throw new NotImplementedException();
}
private void LoadData()
{
context.Machines.Load();
this.DataContext = context.Machines.Local;
}
private void ClearText()
{
MachineID = string.Empty;
CustomerID = string.Empty;
CustomerName = string.Empty;
City = string.Empty;
Country = string.Empty;
SpindleC1 = string.Empty;
SpindleC2 = string.Empty;
HoningHead = string.Empty;
NCVersion = string.Empty;
HMIVersion = string.Empty;
HRIVersion = string.Empty;
AHSVersion = string.Empty;
}
What I have tried:
I try to do it with eventrigger for selectionchanged command and several differnet ways i saw in other questions but i was unlucky : (