加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何使用MVP中的模型填充数据网格视图

发布时间:2020-12-15 22:03:41 所属栏目:百科 来源:网络整理
导读:在win表单应用程序中,UI中有一个网格视图,应该填充客户端分支机构的详细信息.大约有50个分支机构.因此,DataService类中的GetBranchDetails()方法返回一个DataSet,Presenter类将该DataSet传递给UI的BindData()方法,其中网格视图的datasource属性设置为DataSet
在win表单应用程序中,UI中有一个网格视图,应该填充客户端分支机构的详细信息.大约有50个分支机构.因此,DataService类中的GetBranchDetails()方法返回一个DataSet,Presenter类将该DataSet传递给UI的BindData()方法,其中网格视图的datasource属性设置为DataSet对象,如代码所示.

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

DataService类

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

演讲者课程

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

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

查看/ UI

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

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

解决方法

使用此方法将ADO.NET DataTable转换为可以在MVP视图中显示的JSON:

string GetJson(DataTable table)
    {
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        List<Dictionary<string,object>> rows = new List<Dictionary<string,object>>();
        Dictionary<string,object> row = null;
        foreach (DataRow dataRow in table.Rows)
        {
            row = new Dictionary<string,object>();
            foreach (DataColumn column in table.Columns)
            {
                row.Add(column.ColumnName.Trim(),dataRow[column]);
            }
            rows.Add(row);
        }
        return serializer.Serialize(rows);
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读