Harsh Shah

Harsh Shah

  • NA
  • 1.2k
  • 7k

How to select data from DataGrid in WPF MVVM with database

Nov 29 2019 12:13 AM
Hi
I am working in wpf with mvvm light. I want to select data from data grid and put them in textboxes. 
 
let me explain my project scenario.
UI:
  1. <Grid ShowGridLines="True">  
  2.         <Grid.RowDefinitions>  
  3.             <RowDefinition>RowDefinition>  
  4.             <RowDefinition>RowDefinition>  
  5.         Grid.RowDefinitions>  
  6.         <StackPanel Orientation="Vertical" HorizontalAlignment="Center"  
  7.                     Grid.Row="0"   
  8.                     VerticalAlignment="Center">  
  9.             <StackPanel Orientation="Horizontal" Margin="0,0,0,10">  
  10.                 <TextBlock Name="name" Text="Name:" Margin="0,0,30,0">TextBlock>  
  11.                 <TextBox x:Name="text1"   
  12.                          Text="{Binding UserLoginData.Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  
  13.                          Width="100">TextBox>  
  14.             StackPanel>  
  15.   
  16.             <StackPanel Orientation="Horizontal" Margin="0,0,0,10">  
  17.                 <TextBlock Name="password" Text="Password:" Margin="0,0,12,0">TextBlock>  
  18.                 <TextBox x:Name="text2"   
  19.                          Text="{Binding UserLoginData.Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  
  20.                          Width="100">TextBox>  
  21.             StackPanel>  
  22.   
  23.             <StackPanel Orientation="Horizontal" Margin="0,0,0,0">  
  24.                 <Button Content="Login" Margin="0,0,25,0" Padding="5"  
  25.                         Command="{Binding SaveCommand}">  
  26.                 Button>                  
  27.             StackPanel>  
  28.         StackPanel>  
  29.              
  30.         <DataGrid Grid.Row="1" x:Name="MainDataGrid"  
  31.                 ItemsSource="{Binding users}"   
  32.                   SelectedItem="{Binding Path=SelectedUsers,Mode=TwoWay}" SelectionChanged="MainDataGrid_SelectionChanged">  
  33.         DataGrid>  
  34.     Grid>    
I have two text boxes here Name and password. When we values of them that values sotres in my database and add to Datagrid as below image.
 
 
Now what i want is when we select on one of the data in Datagrid it should be add into that two text boxes so i can perform update, delete from it. but here i couldn't get data from datagrid in text boxes when i select on grid. 
I am providing you my viewmodel, datacontext and model class here. so you can find solution from it.
 
View model class: 
  1. public class UserLoginViewModel:ViewModelBase  
  2.     {  
  3.   
  4.         public UserLoginViewModel()  
  5.         {  
  6.             UserLoginData = new UserLoginData();  
  7.             users = new ObservableCollection(GetUsers());  
  8.         }  
  9.   
  10.         internal IEnumerable GetUsers()  
  11.         {  
  12.             DataBaseContext baseContext = new DataBaseContext();  
  13.             return baseContext.userLogins.ToList();  
  14.         }  
  15.   
  16.         private ObservableCollection _SelectedUsers;  
  17.         public ObservableCollection SelectedUsers  
  18.         {  
  19.             get => _SelectedUsers;  
  20.             set  
  21.             {  
  22.                 if(value != _SelectedUsers)  
  23.                 {  
  24.                     _SelectedUsers = value;  
  25.                     RaisePropertyChanged();  
  26.                 }  
  27.             }  
  28.         }  
  29.   
  30.         private ObservableCollection _users;  
  31.         public ObservableCollection users  
  32.         {  
  33.             get => _users;  
  34.             set { _users = value; RaisePropertyChanged(); }  
  35.         }  
  36.   
  37.         private UserLoginData _UserLoginData;  
  38.         public UserLoginData UserLoginData  
  39.         {  
  40.             get => _UserLoginData;  
  41.             set { _UserLoginData = value; RaisePropertyChanged(); }  
  42.         }  
  43.   
  44.         public ICommand SaveCommand => new RelayCommand(SaveData);  
  45.   
  46.         public object ObservableCollectoin { getprivate set; }  
  47.   
  48.         private void SaveData()  
  49.         {  
  50.             DataBaseContext baseContext = new DataBaseContext();  
  51.             baseContext.userLogins.Add(UserLoginData);  
  52.             baseContext.SaveChanges();  
  53.         }           
  54.         }          
  55.     }  
 Datacontext:
  1. public class DataBaseContext: DbContext  
  2.    {  
  3.        public DataBaseContext() : base("DbBindingContext")  
  4.        {  
  5.        }  
  6.   
  7.        public DbSet userLogins { getset; }  
  8.    }  
 Model:
  1. public class UserLoginData  
  2.    {  
  3.        [Key]  
  4.        public int Id { getset; }  
  5.        public string Name { getset; }  
  6.        public string Password { getset; }  
  7.    }  
tell me where i am wrong. and what shoud i do. 
hope will get better solution. Thanks in advance.
 
 

Answers (4)