TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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:
<
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();
}
}
}
Datacontext:
public
class
DataBaseContext: DbContext
{
public
DataBaseContext() :
base
(
"DbBindingContext"
)
{
}
public
DbSet
userLogins {
get
;
set
; }
}
Model:
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.
Reply
Answers (
4
)
Resize the font in a datagrid cell so it fits the cell.
MVVM Binding for Datagrid column inside itemscontrol