在win表单应用程序中,UI中有一个网格视图,应该填充客户端分支机构的详细信息。大约有50个分支机构。因此,GetBranchDetails()
类中的DataService
方法返回DataSet
,而Presenter
类将DataSet
类传递给用户界面BindData()
方法其中网格视图的datasource
属性设置为DataSet
对象,如代码所示。
问题是DataService
没有返回模型而是返回DataSet
。有人可以告诉我如何使用BranchOffice
模型而不是在这里使用DataSet
吗?因此Model
应该从DataService
返回,然后presenter
会将Model
传递给将Model
设置为{{1}的用户界面到网格视图。请注意,总会有不止一个分支机构!
DataService类
datasource
演示者类
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);
}
答案 0 :(得分:1)
使用此方法将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);
}