如何使用MVP中的模型填充数据网格视图 [英] How to populate a data grid view using a model in MVP

查看:112
本文介绍了如何使用MVP中的模型填充数据网格视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在win表单应用程序中,UI中有一个网格视图,应该填充客户端分支机构的详细信息。大约有50个分支机构。所以`DataService`类中的`GetBranchDetails()`方法返回一个`DataSet`,`Presenter`类将`DataSet`传递给UI的`BindData()`方法,其中网格视图的`datasource`属性是设置为`DataSet`对象,如代码所示。



问题是`DataService`没有返回模型而是返回`DataSet`。有人可以告诉我如何使用`BranchOffice`模型而不是在这里使用`DataSet`?因此`Model`应该从`DataService`返回,然后`presenter`将把'Model`传递给UI,其中`Model`将被设置为网格视图的`datasource`。请注意,总会有不止一个分支机构!



数据服务类





In a win forms application there is a grid view in the UI which should be populated with the details of branch offices of the client. There are about 50 branches. So `GetBranchDetails()` method in the `DataService` class returns a `DataSet` and the `Presenter` class passes that `DataSet` to the UI's `BindData()` method in which the `datasource` property of the grid view is set to the `DataSet` object as shown in the code.

The problem is the `DataService` is not returning a model rather it returns a `DataSet`. Can some one tell me how to use `BranchOffice` model instead of using a `DataSet` here? So that the `Model` should be returned from `DataService` and then the `presenter` will pass that `Model` to UI where the `Model` will be set as the `datasource` to the grid view. Please note that always there will be more than one branch offices!

DataService Class


public DataTable GetBranchDetails()
{
    string selectStatement = "SELECT ID, branch_name + ', ' + branch_add AS Branch,       area, sm, atten_status FROM Branch 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;
   }
}







Presenter Class






Presenter Class

private void _View_ShowBranches(object sender, EventArgs e)
{
    ShowBranches();
}

private void ShowBranches()
{
    var dt = DataService.GetBranchDetails();
    this._View.BindData(dt);
}







查看/用户界面






View / UI

public partial class frmAttendancePoints : Form, IView
{
    public frmAttendancePoints()
    {
        InitializeComponents();
    }

    public void BindData(DataSet dt)
    {
        dgAttendancePoints.DataSource = dt;
    }
}

推荐答案

这篇关于如何使用MVP中的模型填充数据网格视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆