This is a migrated thread and some comments may be shown as answers.

INotifyPropertyChanged and sql link

3 Answers 152 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chom
Top achievements
Rank 1
Chom asked on 10 Mar 2014, 02:46 PM
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



3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 12 Mar 2014, 03:35 PM
Hello Gavin,

Thank you for writing.

The idea of the BindingList and INotifyPropetyChanged is that when a change in the object or the list with the objects appears, this change will be reflected by RadGridView. Please refer to the following article which describes this case in detail: http://www.telerik.com/help/winforms/gridview-populating-with-data-reflecting-custom-object-changes-in-rgv.html

In regards to proper binding to entities, please refer to the following article: http://www.telerik.com/help/winforms/gridview-populating-with-data-binding-to-entity-framework-using-database-first-approach.html.

Should you have any other questions or suggestions, do not hesitate to contact us. 

Regards,
Stefan
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
0
Chom
Top achievements
Rank 1
answered on 19 Mar 2014, 11:13 AM
Hello Stefan,

Thanks for the help. I also found this webinar very helpful http://www.telerik.com/help/winforms/gridview-overview.html.

Regards,
Gavin
0
George
Telerik team
answered on 24 Mar 2014, 10:56 AM
Hi Chom,

I am glad that you found our documentation useful.

Do not hesitate to contact us if you require any further assistance.

Regards,
George
Telerik
 

Build cross-platform mobile apps using Visual Studio and .NET. Register for the online webinar on 03/27/2014, 11:00AM US ET.. Seats are limited.

 
Tags
GridView
Asked by
Chom
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Chom
Top achievements
Rank 1
George
Telerik team
Share this question
or