0

how populate ComboBox and DataGridView using MVP (Model-View-Presenter). Actually i have something like this:

The View base class:

public interface IView
{
    event EventHandler Initialize;
    event EventHandler Load;
}

The presenter base class:

public class Presenter<TView> where TView : class, IView
{
    private TView view;
    public TView View { get { return view; } private set { view = value; } }

    public Presenter(TView view)
    {
        if (view == null)
            throw new ArgumentNullException("view");

        View = view;
        View.Initialize += OnViewInitialize;
        View.Load += OnViewLoad;
    }

    protected virtual void OnViewInitialize(object sender, EventArgs e) { }

    protected virtual void OnViewLoad(object sender, EventArgs e) { }
}

The specific view:

public interface IAdministrarUsuariosView : IView
{

    string NombreUsuarioABuscar {get; set;}

    List<Perfil> ListaPerfiles {get; set;}

    event EventHandler BuscarUsuarioPorNombre;
    event EventHandler BuscarUsuarioPorPerfil;
}

I don't know how to populate the ComboBox and the Datagridview!

PD: Thanks to Josh for the code of the View and Presenter base classes (MVP Base Class)

Thanks!!

Community
  • 1
  • 1
fcartu
  • 355
  • 1
  • 9
  • 20

1 Answers1

2

you need to create a property that you will use to set up the data source for the ComboBox and DropdownList.

just to give you an example(you need to improve this code but it shows a way on how you can do that)

in you view :

//this is just a template to simulate a datasource item
public class TestItem
{
    public int Id { get; set; }
    public string Description { get; set; }
}

public interface IAdministrarUsuariosView : IView
{

    string NombreUsuarioABuscar { get; set; }

   // List<Perfil> ListaPerfiles { get; set; }

    event EventHandler BuscarUsuarioPorNombre;
    event EventHandler BuscarUsuarioPorPerfil;
    List<TestItem> SetComboBox { set; }
    List<TestItem> SetGridView { set; }

}

then in the concrete view (the winform that imolements the IAdministrarUsuariosView

public class YourView:IAdministrarUsuariosView
{
    public string NombreUsuarioABuscar
    {
        get { throw new NotImplementedException(); }
        set { throw new NotImplementedException(); }
    }

    public event EventHandler BuscarUsuarioPorNombre;
    public event EventHandler BuscarUsuarioPorPerfil;

    public List<TestItem> SetComboBox
    {
        set
        {
            ComboBox.DataSource = value;
            //your need to specify value and text property
            ComboBox.DataBind();
        }
    }

    public List<TestItem> SetGridView
    {
        set
        {
            GridView.DataSource = value;
            //your need to specify value and text property
            GridView.DataBind();

        }
    }
}

then your presenter should look like the below:

public class YourPresenter:Presenter<IAdministrarUsuariosView>
{

    public YourPresenter(IAdministrarUsuariosView view) : base(view)
    {
    }

    protected override void OnViewLoad(object sender, EventArgs e)
    {
        List<TestItem> listResult = GetListItem();
        this.View.SetComboBox = listResult;
        this.View.SetGridView = listResult;
    }
}
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • Hi Massimiliano, thanks for your help, i didn´t know it was so simple, thanks very much for your help!! – fcartu Aug 02 '11 at 00:11
  • hi Massimiliano, will you please explain why you crated a presenter as genric rather than just simple base class? means i whats the benefit of using it as generic or how it will works? – 0cool Feb 02 '12 at 06:13
  • YourPresenter:Presenter i am bit confused about this line that we are passing IAdministrarUsuariosView interface to generic class - public class Presenter where TView : class, IView – 0cool Feb 02 '12 at 06:26