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
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;
}
}
Use this method to convert the ADO.NET DataTable to JSON that can be presented in an MVP View:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.