Hello,
I am trying to figure out the INotifyPropertyChangd interface for using a Binding list with a grid. I CRUD my data to the database with code and display in the grid but I have to call a method to load the data into the grid every time I make a change. I am missing something to bind my data to the grid whenever a change is made. I created some small demo files and have looked at multiple examples but am still having problems figuring the link between the db and the grid. Below is what I have created to help understand the concept. One thing to note is that I am creating my data table using ORM fluent mapping in code.
//public class _Student
public class _Student : System.ComponentModel.INotifyPropertyChanged
{
private int id;
private string name;
private string grade;
public int Id
{
get { return id; }
set
{
id = value;
this.OnPropertyChanged("Id");
}
}
public string Name
{
get { return name; }
set
{
name = value;
this.OnPropertyChanged("Name");
}
}
public string Grade
{
get { return grade; }
set
{
grade = value;
this.OnPropertyChanged("Grade");
}
}
private void OnPropertyChanged(string PropertyName)
{
this.OnPropertyChanged(new PropertyChangedEventArgs(PropertyName));
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
if (PropertyChanged != null)
{
PropertyChanged(this, args);
}
}
public event PropertyChangedEventHandler PropertyChanged;
//Mapping.
internal static MappingConfiguration<_Student> StudentMapping()
{
var config = new MappingConfiguration<_Student>();
config.MapType(x => new
{
Name = x.Name,
Grade = x.Grade,
}).ToTable("Student");
config.HasProperty(p => p.Id).IsIdentity(KeyGenerator.Autoinc);
config.HasProperty(p => p.Name).IsNotNullable().IsUnicode().HasLength(50);
config.HasProperty(p => p.Grade).IsNullable().IsUnicode().HasLength(50);
return config;
}
}
public class _vmstudent
{
//INSERT.
public static void InsertNewStudentInfo(BindingList<_Student> stud)
{
using (ProjectSpecSqlFluentContext db = new ProjectSpecSqlFluentContext())
{
foreach (var item in stud)
{
if (!db._Students.Any(x => x.Name.Equals(item.Name)))
{
_Student s = new _Student
{
Name = item.Name,
Grade = item.Grade
};
db.Add(s);
}
}
db.SaveChanges();
}
}
//UPDATE.
public static void UpdateStudentInfo(int Id, string BestGrade)
{
using (ProjectSpecSqlFluentContext db = new ProjectSpecSqlFluentContext())
{
if (db._Students.Any(x => x.Id.Equals(Id)))
{
_Student s = db._Students.Single(x => x.Id.Equals(Id));
s.Grade = BestGrade;
db.SaveChanges();
}
}
}
//READ.
public static BindingList<_Student> ReadStudentInfo()
{
BindingList<_Student> list;
using (ProjectSpecSqlFluentContext db = new ProjectSpecSqlFluentContext())
{
var result = (from s in db._Students
select new _Student
{
Id = s.Id,
Name = s.Name,
Grade = s.Grade
}).ToList();
list = new BindingList<_Student>(result);
}
return list;
}
//DELETE.
public static void DeleteStudentInfo(int Id)
{
using (ProjectSpecSqlFluentContext db = new ProjectSpecSqlFluentContext())
{
if (db._Students.Any(x => x.Id.Equals(Id)))
{
_Student s = db._Students.FirstOrDefault(x => x.Id.Equals(Id));
db.Delete(s);
db.SaveChanges();
}
}
}
}
public partial class StudentForm : Telerik.WinControls.UI.RadForm
{
private BindingList<_Student> student;
public StudentForm()
{
InitializeComponent();
////Default schema.
//vmSchema.AddDefaultTableInfo();
this.radGridView1.DataSource = _vmstudent.ReadStudentInfo();
this.rbtnAdd.Click += rbtnAdd_Click;
this.rbtnDelete.Click += rbtnDelete_Click;
this.rbtnBestGrade.Click += rbtnBestGrade_Click;
}
//ADD...
void rbtnAdd_Click(object sender, EventArgs e)
{
student = new BindingList<_Student>();
student.Add(new _Student { Id = 0, Name = "Peter", Grade = "A" });
student.Add(new _Student { Id = 0, Name = "John", Grade = "B" });
student.Add(new _Student { Id = 0, Name = "Tony", Grade = "C" });
student.Add(new _Student { Id = 0, Name = "David", Grade = "D" });
_vmstudent.InsertNewStudentInfo(student);
this.radGridView1.DataSource = _vmstudent.ReadStudentInfo();
}
//BEST GRADE...
void rbtnBestGrade_Click(object sender, EventArgs e)
{
int index = this.radGridView1.Rows.IndexOf(radGridView1.CurrentRow);
string id = this.radGridView1.Rows[index].Cells["Id"].Value.ToString();
_vmstudent.UpdateStudentInfo(Convert.ToInt16(id), txtBestGrade.Text);
this.radGridView1.DataSource = _vmstudent.ReadStudentInfo();
}
//DELETE..
void rbtnDelete_Click(object sender, EventArgs e)
{
int index = this.radGridView1.Rows.IndexOf(radGridView1.CurrentRow);
string id = this.radGridView1.Rows[index].Cells["Id"].Value.ToString();
_vmstudent.DeleteStudentInfo(Convert.ToInt16(id));
this.radGridView1.DataSource = _vmstudent.ReadStudentInfo();
}
}
Your help would be appreciated.
Thanks
Gavin
I am trying to figure out the INotifyPropertyChangd interface for using a Binding list with a grid. I CRUD my data to the database with code and display in the grid but I have to call a method to load the data into the grid every time I make a change. I am missing something to bind my data to the grid whenever a change is made. I created some small demo files and have looked at multiple examples but am still having problems figuring the link between the db and the grid. Below is what I have created to help understand the concept. One thing to note is that I am creating my data table using ORM fluent mapping in code.
//public class _Student
public class _Student : System.ComponentModel.INotifyPropertyChanged
{
private int id;
private string name;
private string grade;
public int Id
{
get { return id; }
set
{
id = value;
this.OnPropertyChanged("Id");
}
}
public string Name
{
get { return name; }
set
{
name = value;
this.OnPropertyChanged("Name");
}
}
public string Grade
{
get { return grade; }
set
{
grade = value;
this.OnPropertyChanged("Grade");
}
}
private void OnPropertyChanged(string PropertyName)
{
this.OnPropertyChanged(new PropertyChangedEventArgs(PropertyName));
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
if (PropertyChanged != null)
{
PropertyChanged(this, args);
}
}
public event PropertyChangedEventHandler PropertyChanged;
//Mapping.
internal static MappingConfiguration<_Student> StudentMapping()
{
var config = new MappingConfiguration<_Student>();
config.MapType(x => new
{
Name = x.Name,
Grade = x.Grade,
}).ToTable("Student");
config.HasProperty(p => p.Id).IsIdentity(KeyGenerator.Autoinc);
config.HasProperty(p => p.Name).IsNotNullable().IsUnicode().HasLength(50);
config.HasProperty(p => p.Grade).IsNullable().IsUnicode().HasLength(50);
return config;
}
}
public class _vmstudent
{
//INSERT.
public static void InsertNewStudentInfo(BindingList<_Student> stud)
{
using (ProjectSpecSqlFluentContext db = new ProjectSpecSqlFluentContext())
{
foreach (var item in stud)
{
if (!db._Students.Any(x => x.Name.Equals(item.Name)))
{
_Student s = new _Student
{
Name = item.Name,
Grade = item.Grade
};
db.Add(s);
}
}
db.SaveChanges();
}
}
//UPDATE.
public static void UpdateStudentInfo(int Id, string BestGrade)
{
using (ProjectSpecSqlFluentContext db = new ProjectSpecSqlFluentContext())
{
if (db._Students.Any(x => x.Id.Equals(Id)))
{
_Student s = db._Students.Single(x => x.Id.Equals(Id));
s.Grade = BestGrade;
db.SaveChanges();
}
}
}
//READ.
public static BindingList<_Student> ReadStudentInfo()
{
BindingList<_Student> list;
using (ProjectSpecSqlFluentContext db = new ProjectSpecSqlFluentContext())
{
var result = (from s in db._Students
select new _Student
{
Id = s.Id,
Name = s.Name,
Grade = s.Grade
}).ToList();
list = new BindingList<_Student>(result);
}
return list;
}
//DELETE.
public static void DeleteStudentInfo(int Id)
{
using (ProjectSpecSqlFluentContext db = new ProjectSpecSqlFluentContext())
{
if (db._Students.Any(x => x.Id.Equals(Id)))
{
_Student s = db._Students.FirstOrDefault(x => x.Id.Equals(Id));
db.Delete(s);
db.SaveChanges();
}
}
}
}
public partial class StudentForm : Telerik.WinControls.UI.RadForm
{
private BindingList<_Student> student;
public StudentForm()
{
InitializeComponent();
////Default schema.
//vmSchema.AddDefaultTableInfo();
this.radGridView1.DataSource = _vmstudent.ReadStudentInfo();
this.rbtnAdd.Click += rbtnAdd_Click;
this.rbtnDelete.Click += rbtnDelete_Click;
this.rbtnBestGrade.Click += rbtnBestGrade_Click;
}
//ADD...
void rbtnAdd_Click(object sender, EventArgs e)
{
student = new BindingList<_Student>();
student.Add(new _Student { Id = 0, Name = "Peter", Grade = "A" });
student.Add(new _Student { Id = 0, Name = "John", Grade = "B" });
student.Add(new _Student { Id = 0, Name = "Tony", Grade = "C" });
student.Add(new _Student { Id = 0, Name = "David", Grade = "D" });
_vmstudent.InsertNewStudentInfo(student);
this.radGridView1.DataSource = _vmstudent.ReadStudentInfo();
}
//BEST GRADE...
void rbtnBestGrade_Click(object sender, EventArgs e)
{
int index = this.radGridView1.Rows.IndexOf(radGridView1.CurrentRow);
string id = this.radGridView1.Rows[index].Cells["Id"].Value.ToString();
_vmstudent.UpdateStudentInfo(Convert.ToInt16(id), txtBestGrade.Text);
this.radGridView1.DataSource = _vmstudent.ReadStudentInfo();
}
//DELETE..
void rbtnDelete_Click(object sender, EventArgs e)
{
int index = this.radGridView1.Rows.IndexOf(radGridView1.CurrentRow);
string id = this.radGridView1.Rows[index].Cells["Id"].Value.ToString();
_vmstudent.DeleteStudentInfo(Convert.ToInt16(id));
this.radGridView1.DataSource = _vmstudent.ReadStudentInfo();
}
}
Your help would be appreciated.
Thanks
Gavin