How to Bind Data and manipulate it in a GridView with MVP
- by DotNetDan
I am new to the whole MVP thing and slowly getting my head around it all. The a problem I am having is how to stay consistent with the MVP methodology when populating GridViews (and ddls, but we will tackle that later).
Is it okay to have it connected straight to an ObjectDataSourceID? To me this seems wrong because it bypasses all the separation of concerns MVP was made to do.
So, with that said, how do I do it? How do I handle sorting (do I send over handler events to the presentation layer, if so how does that look in code)?
Right now I have a GridView that has no sorting. Code below.
ListCustomers.aspx.cs:
public partial class ListCustomers : System.Web.UI.Page, IlistCustomer
{
protected void Page_Load(object sender, EventArgs e)
{
//On every page load, create a new presenter object with
//constructor recieving the
// page's IlistCustomer view
ListUserPresenter ListUser_P = new ListUserPresenter(this);
//Call the presenter's PopulateList to bind data to gridview
ListUser_P.PopulateList();
}
GridView IlistCustomer.UserGridView
{
get { return gvUsers; }
set { gvUsers = value; }
}
}
Interface ( IlistCustomer.cs): is this bad sending in an entire Gridview control?
public interface IlistCustomer
{
GridView UserGridView { set; get; }
}
The Presenter (ListUserPresenter.cs):
public class ListUserPresenter
{
private IlistCustomer view_listCustomer;
private GridView gvListCustomers;
private DataTable objDT;
public ListUserPresenter( IlistCustomer view)
{
//Handle an error if an Ilistcustomer was not sent in)
if (view == null)
throw new ArgumentNullException("ListCustomer View cannot be blank");
//Set local IlistCustomer interface view
this.view_listCustomer = view;
}
public void PopulateList()
{
//Fill local Gridview with local IlistCustomer
gvListCustomers = view_listCustomer.UserGridView;
// Instantiate a new CustomerBusiness object to contact database
CustomerBusiness CustomerBiz = new CustomerBusiness();
//Call CustomerBusiness's GetListCustomers to fill DataTable object
objDT = CustomerBiz.GetListCustomers();
//Bind DataTable to gridview;
gvListCustomers.DataSource = objDT;
gvListCustomers.DataBind();
}
}