I'm developing a win forms application in MVP pattern. In the application, there is a small module to show the branch offices (called points) where the attendance entering process has been completed. Those details should be shown in a data grid view.
So I have a form (View), a presenter and a data service classes. For the simplicity I'm not using a "model class" for this as the only intended functionality is populating the grid view.
Form frmAttendancePoints
and DataService
class have implemented IAttendancePointsView
and IDataService
interfaces respectively.
My question: Is the way I'm populating the grid correct? If not could you please let me know a better solution.
View
public partial class frmAttendancePoints : Form, IAttendancePointsView
{
private void frmEnterAttendance_Load(object sender, EventArgs e)
{
ShowAttendancePoints(sender, e);
}
public void LoadAttendacePointsGridWithData(DataTable dt)
{
dgAttendancePoints.DataSource = dt;
}
public event EventHandler ShowAttendancePoints;
}
Presenter
class AttendancePointPresenter : BasePresenter
{
IAttendancePointsView _View;
IDataService _ds;
public AttendancePointPresenter(IAttendancePointsView View, IDataService ds)
{
_View = View;
_ds = ds;
WireUpViewEvents();
}
private void WireUpViewEvents()
{
this._View.ShowAttendancePoints +=new EventHandler(_View_ShowAttendancePoints);
}
private void _View_ShowAttendancePoints(object sender, EventArgs e)
{
ShowAttendancePoints();
}
private void ShowAttendancePoints()
{
var dt = _ds.GetAttendancePoints();
this._View.LoadAttendacePointsGridWithData(dt);
}
public void Show()
{
_View.ShowDialog();
}
}
DataService
public class DataServie
{
public DataTable GetAttendancePoints()
{
string selectStatement = "SELECT PID, point_name + ', ' + point_add AS point, area, sm, atten_status FROM Point WHERE Status='Active'";
using (SqlConnection sqlConnection = new SqlConnection(db.GetConnectionString))
{
using (SqlCommand sqlCommand = new SqlCommand(selectStatement, sqlConnection))
{
using (SqlDataAdapter da = new SqlDataAdapter(sqlCommand))
{
using (DataSet ds = new DataSet())
{
using (DataTable dt = new DataTable())
{
ds.Tables.Add(dt);
sqlConnection.Open();
da.Fill(dt);
return dt;
}
}
}
}
}
}
}
Programme
IAttendancePointsView AttendancePointView = new frmAttendancePoints();
IDataService ds = new DataService();
var Presenter = new AttendancePointPresenter(AttendancePointView, ds);
Presenter.Show();