I'm new to Databinding in .NET and I want to bind my DataGridView to a object list. This grid may populate in two ways.
There are TextBoxes in the form and users can enter text and when they press Add button a new object is instantiated with the provided fields and it is added to the list.
Other way is, when a user searches for objects, matching objects are loaded from database to the list. Also after searching users should be able to add new items to this list as in the first method.
Finally this list is binded to the DataGridView and now user could do any changes to the items shown in the grid and if it's all right, user could save them.
So in my presenter I have three methods for above requirements.
Class AttendancePresenter
{
private void AddAttendance()
{
AttendanceList.Add(attendanceModel);
var bindingList = new BindingList<IAttendance>(AttendanceList);
var source = new BindingSource(bindingList, null);
_View.AttendanceGrid = source;
}
private void GetAttendance()
{
AttendanceList = _DataService.GetAttendance();
var bindingList = new BindingList<IAttendance>(AttendanceList);
var source = new BindingSource(bindingList, null);
_View.AttendanceInGrid = source;
}
private void Save()
{
_DataService.InsertAttendance (AttendanceList);
}
}
I'm not sure the way I've binded things is right. And also I wonder if I could use a single binding list here as I'm binding same list to the same DataGridView! Is it possible?
Whats the difference of using BindingSource along and with a BindingList, in this context?