HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Binding a DataGridView to a List

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
listdatagridviewbinding

Problem

I'm new to Databinding in MVP and I want to bind my DataGridView to a object list. This grid may be populated 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 then 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 details are shown in the grid. Now the user could do any changes to the items shown in the grid and if it's all right, the user could save them.

So in my presenter I have three methods for the above requirements.

```
Class AttendancePresenter : BasePresenter
{
private IAttendance _Model;
private readonly IAttendanceView _View;
private readonly IDataService _DataService;
private readonly IMessageService _MessageService;
List AttendanceList = new List();

public AttendancePresenter( IAttendance model, IAttendanceView view, IDataService dataService, IMessageService messageService ) : base(messageService)
{
_Model = model;
_View = view;
_DataService = dataService;
_MessageService = messageService;

SetModelPropertiesFromView(_Model, _View);
WireUpViewEvents();
}

//Add new attendance to the list and the the list is binded to the grid
private void _View_OnAddAttendance(object sender, EventArgs e)
{

SetModelPropertiesFromView(_Model, _View); // call base class method to update the Mode with uptodate data from the view
IAttendance attendanceModel = new Attendance(_Model); // Use Model's copy constructor to get a clone
AttendanceList.Add(attendanceModel);
var bindingList = new BindingList(AttendanceList);

Solution

First some nitpicks:

-
Variables should be camelCased, not PascalCased. The underscore prefixes are okay, but _Model is not. Let the underscore be your visual indicator. What you've done is confusing on the eyes.

-
Don't fully qualify names unless you have to.

var bindingList = new System.ComponentModel.BindingList(AttendanceList);


Else where you directly use BindingList, so System.ComponentModel must be in your usings somewhere. Using the fully qualified call clutters the code.

-
You could extract this duplicated code into its own method.

var bindingList = new BindingList(AttendanceList);
var source = new BindingSource(bindingList, null);
_View.AttendanceGrid = source;


I don't see anything particularly bad with what you've done, but perhaps I'm inexperienced or lacking larger context. The only thing I might recommend, would be to keep a bindingList around and just call the clear method instead of creating a new instance from scratch all the time. Let me be clear though, I'm not 100% sure that's good advice or any better than "newing it up".

Code Snippets

var bindingList = new System.ComponentModel.BindingList<IAttendance>(AttendanceList);
var bindingList = new BindingList<IAttendance>(AttendanceList);
var source = new BindingSource(bindingList, null);
_View.AttendanceGrid = source;

Context

StackExchange Code Review Q#55492, answer score: 2

Revisions (0)

No revisions yet.