DataGridEx.xaml.cs
public partial class DataGridEx : Window
{
public DataGridEx()
{
InitializeComponent();
}
}
DataGridExViewModel.cs
public class DataGridExViewModel : BindableBase
{
public DataGridExViewModel()
{
LoadEmployees();
LstEmployees.Select(x =>
{
x.IsValueChanged = false;
return x;
}).ToList();
}
private ObservableCollection<Employees> _lstEmployees = new ObservableCollection<Employees>();
public ObservableCollection<Employees> LstEmployees
{
get { return _lstEmployees; }
set { SetProperty(ref _lstEmployees, value); }
}
private DelegateCommand<Employees> _updateCommand;
public DelegateCommand<Employees> UpdateCommand =>
_updateCommand ?? (_updateCommand = new DelegateCommand<Employees>(Update));
void Update(Employees employee)
{
using(var sqlite = new SqliteOperation(@"\Projects\emp.db"))
{
sqlite.Update(employee);
}
LstEmployees.Select(x =>
{
if(x.EmpId == employee.EmpId)
x.IsValueChanged = false;
return x;
}).ToList();
}
private void LoadEmployees()
{
using(var sqlite = new SqliteOperation(@"\Projects\emp.db"))
{
LstEmployees.AddRange(sqlite.Get());
}
}
}
SqliteOperation.cs
public class SqliteOperation : IDisposable
{
public void Dispose()
{
GC.SuppressFinalize(this);
}
private readonly SQLiteConnection connection;
public SqliteOperation(string connectionString)
{
connection = new SQLiteConnection(connectionString);
connection.CreateTable<Employees>();
}
public void Add(Employees employee)
{
connection.Insert(employee);
}
public void Update(Employees employee)
{
connection.Update(employee);
}
public List<Employees> Get()
{
return connection.Table<Employees>().ToList();
}
}
Employees.cs
public class Employees : BindableBase
{
private int _empId;
[AutoIncrement, PrimaryKey]
public int EmpId
{
get { return _empId; }
set { SetProperty(ref _empId, value); }
}
private string _empName;
[NotNull]
public string EmpName
{
get { return _empName; }
set
{
if(_empName != value)
IsValueChanged = true;
SetProperty(ref _empName, value);
}
}
private string _jobTitle;
[NotNull]
public string JobTitle
{
get { return _jobTitle; }
set
{
if(_jobTitle != value)
IsValueChanged = true;
SetProperty(ref _jobTitle, value);
}
}
private string _experience;
[NotNull]
public string Experience
{
get { return _experience; }
set
{
if(_experience != value)
IsValueChanged = true;
SetProperty(ref _experience, value);
}
}
private double _salary;
[NotNull]
public double Salary
{
get { return _salary; }
set
{
if(_salary != value)
IsValueChanged = true;
SetProperty(ref _salary, value);
}
}
private bool _isValueChanged;
[Ignore]
public bool IsValueChanged
{
get { return _isValueChanged; }
set { SetProperty(ref _isValueChanged, value); }
}
}
Comments
Post a Comment