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:
- <Grid ShowGridLines="True">
- <Grid.RowDefinitions>
- <RowDefinition>
RowDefinition> - <RowDefinition>
RowDefinition> -
Grid.RowDefinitions> - <StackPanel Orientation="Vertical" HorizontalAlignment="Center"
- Grid.Row="0"
- VerticalAlignment="Center">
- <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
- <TextBlock Name="name" Text="Name:" Margin="0,0,30,0">
TextBlock> - <TextBox x:Name="text1"
- Text="{Binding UserLoginData.Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
- Width="100">
TextBox> -
StackPanel> - <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
- <TextBlock Name="password" Text="Password:" Margin="0,0,12,0">
TextBlock> - <TextBox x:Name="text2"
- Text="{Binding UserLoginData.Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
- Width="100">
TextBox> -
StackPanel> - <StackPanel Orientation="Horizontal" Margin="0,0,0,0">
- <Button Content="Login" Margin="0,0,25,0" Padding="5"
- Command="{Binding SaveCommand}">
-
Button> -
StackPanel> -
StackPanel> - <DataGrid Grid.Row="1" x:Name="MainDataGrid"
- ItemsSource="{Binding users}"
- SelectedItem="{Binding Path=SelectedUsers,Mode=TwoWay}" SelectionChanged="MainDataGrid_SelectionChanged">
-
DataGrid> -
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:
- public class UserLoginViewModel:ViewModelBase
- {
- public UserLoginViewModel()
- {
- UserLoginData = new UserLoginData();
- users = new ObservableCollection
(GetUsers()); - }
- internal IEnumerable
GetUsers() - {
- DataBaseContext baseContext = new DataBaseContext();
- return baseContext.userLogins.ToList();
- }
- private ObservableCollection
_SelectedUsers; - public ObservableCollection
SelectedUsers - {
- get => _SelectedUsers;
- set
- {
- if(value != _SelectedUsers)
- {
- _SelectedUsers = value;
- RaisePropertyChanged();
- }
- }
- }
- private ObservableCollection
_users; - public ObservableCollection
users - {
- get => _users;
- set { _users = value; RaisePropertyChanged(); }
- }
- private UserLoginData _UserLoginData;
- public UserLoginData UserLoginData
- {
- get => _UserLoginData;
- set { _UserLoginData = value; RaisePropertyChanged(); }
- }
- public ICommand SaveCommand => new RelayCommand(SaveData);
- public object ObservableCollectoin { get; private set; }
- private void SaveData()
- {
- DataBaseContext baseContext = new DataBaseContext();
- baseContext.userLogins.Add(UserLoginData);
- baseContext.SaveChanges();
- }
- }
- }
- public class DataBaseContext: DbContext
- {
- public DataBaseContext() : base("DbBindingContext")
- {
- }
- public DbSet
userLogins { get; set; } - }
- public class UserLoginData
- {
- [Key]
- public int Id { get; set; }
- public string Name { get; set; }
- public string Password { get; set; }
- }
tell me where i am wrong. and what shoud i do.
hope will get better solution. Thanks in advance.

Mohammad TuahaPosted Nov 29, 2019, 8:08 PM
Create a DatabaseLayer.cs class in Data Folder in your application. Write the below code in this class.
classDatabaseLayer{publicstaticList GetEmployeeFromDatabase() {try{DataTable dt = SqlHelper.ExecuteDataTable(AppConstants.getConnectionString(), CommandType.StoredProcedure,"[dbo].[uspGetUser]");varEmployee =newList(); foreach(DataRow rowindt.Rows){varobj =newUser(){ID = (string)row["ID"],FirstName = (string)row["FirstName"],LastName = (string)row["LastName"],DOB = (string)row["DOB"],Gender = (string)row["Gender"],Nationality = (string)row["Nationality"],Language = ((string)row["Language"]),Address = (string)row["Address"],Male = (bool)row["Male"],Female = (bool)row["Female"],Hindi = (bool)row["Hindi"],English = (bool)row["English"],French = (bool)row["French"],};Employee.Add(obj);}returnEmployee;}catch(Exception ex){throwex;}}}Arun Kumar TiwariPosted Nov 29, 2019, 3:39 AM
Ayappan APosted Nov 29, 2019, 3:32 AM
Rajanikant HawaldarPosted Nov 29, 2019, 2:22 AM