开发者

Populate Combobox and DataGridView using MVP

开发者 https://www.devze.com 2023-03-24 23:20 出处:网络
how populate ComboBox and DataGridView using MVP (Model-View-Presenter). Actually i have something like this:

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 virtu开发者_高级运维al 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!!


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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消