Click here to Skip to main content
16,015,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
<!--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:

C#
//Operation button for save, update, delete and print
       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; }

       //Datagrid string

       //PRWContext for general use
       private PRWContext context = new PRWContext();

       public MachineViewModel()
       {
           //Commands for save, update, delete and print
           SaveCommand = new RelayCommand(() => ExecuteSaveCommand());
           UpdateCommand = new RelayCommand(() => ExecuteUpdateCommand());
           DeleteCommand = new RelayCommand(() => ExecuteDeleteCommand());
           PrintCommand = new RelayCommand(() => ExecutePrintCommand());


           ////Load the data from PRW Database to datagrid
           LoadData();
       }

       private void ExecuteSaveCommand()
       {
           Machine machine = new Machine
           {
               //Machine data
               MachineID = MachineID,
               CustomerID = CustomerID,
               CustomerName = CustomerName,
               City = City,
               Country = Country,

               //Serial data
               SpindleC1 = SpindleC1,
               SpindleC2 = SpindleC2,
               HoningHead = HoningHead,

               //Softwareversion data
               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();
       }

       //Load data from database to grid
       private void LoadData()
       {
           context.Machines.Load();
           this.DataContext = context.Machines.Local;
       }

       //Clear textboxes
       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 : (
Posted
Updated 11-May-20 2:18am

1 solution

0) Calling your observable collection "DataContext" is a bad idea, because that's the name the form uses for it's data context property. Things can get real confusing, real fast.

1) You have to bind your textboxes to properties in the selected grid item. In your form, create a new property that represents the currently selected item to make that easier.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900