Datagridview and MVP layer in C# winform

419 views Asked by At

I'm trying to link Data from Database to the Datagridview. There are some problem occurred when I apply MVP to my project. Please help me to solve out this problem. I already divided into 3 folders (Model, View, Presenter). It will look like this:

enter image description here

ConnectDB will connect to the database and get data through the query:

namespace Doan.Model
{
    public class ConnectDB
    {
        SqlConnection connect;

        public ConnectDB()
        {
            this.connect = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=ComesticDB;Integrated Security=True");
        }

        public DataTable GetData(string sqlquery)
        {
            SqlDataAdapter sqldata = new SqlDataAdapter(sqlquery, this.connect);
            DataTable dataTable = new DataTable();
            sqldata.Fill(dataTable);
            return dataTable;
        }
    }
}

This is the model class:

public class Product
{
    public int Product_id;
    public string ProductName;
    public float Price;
    public string Unit;
    public string Description;
    public string Original;
    public int ProductType;

    public DataTable GetProductData()
    {
        ConnectDB connect = new ConnectDB();
        string sqlQuery = "select * from Product";
        return connect.GetData(sqlQuery);
    }
}

This is my interface

public interface IProduct
{
    int ProductID { get; set; }
    string ProductName { get; set; }
    float Price { get; set; }
    string Unit { get; set; }
    string Description { get; set; }
    string Original { get; set; }
    string ProductType { get; set; }
    string message { get; set; }
    DataGridViewRowCollection gvData { get; }
}

This is the presenter:

public class ProductPresenter
{
    IProduct productview;
    Product product = new Product();

    public ProductPresenter(IProduct view)
    {
        productview = view;
    }

    public bool GetProduct()
    {
        foreach(DataRow row in product.GetProductData().Rows)
        {
            productview.gvData.Add(row);
        }

        return true;
    }
}

Here is what I do in form:

namespace Doan.View.Product
{
    public partial class ProductForm : Form, IProduct
    {
        public ProductForm()
        {
            InitializeComponent();
        }

        int IProduct.ProductID 
        { 
            get { return Int32.Parse(txtProductId.Text); } 
            set { txtProductId.Text = value.ToString(); }
        }

        string IProduct.ProductName 
        {
            get { return txtProductName.Text; }
            set { txtProductName.Text = value; } 
        }

        float IProduct.Price
        {
            get { return float.Parse(txtPrice.Text); }
            set { txtPrice.Text = value.ToString(); }
        }

        string IProduct.Unit
        {
            get { return cbUnit.Text; }
            set { cbUnit.Text = value; }
        }

        string IProduct.Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }

        string IProduct.Original
        {
            get { return txtOriginal.Text; }
            set { txtOriginal.Text = value; }
        }

        string IProduct.ProductType
        {
            get { return cbProductType.Text; }
            set { cbProductType.Text = value; }
        }

        private string _message;
        string IProduct.message
        {
            get { return _message; }
            set { _message = value; }
        }

        public DataGridViewRowCollection gvData 
        {
            get { return dtgvProduct.Rows; } 
        }

        private void ProductForm_Load(object sender, EventArgs e)
        {
            ProductPresenter productPresenter = new ProductPresenter(this);
            productPresenter.GetProduct();
        }
    }
}

This is what I received

enter image description here

Thank you very much!

1

There are 1 answers

2
Shen Sudara On

How would you save returned datatable in ProductPresenter class? You need to save datatable in Datatable first running the query. Then you can use foreach loop for view the data.

public class ProductPresenter
    {
        IProduct productview;
        Product product = new Product();
        DataTable dt = new DataTable();
        dt = product.GetProductData();
        public ProductPresenter(IProduct view)
        {
            productview = view;
        }
        public bool GetProduct()
        {
            foreach(DataRow row in dt.Rows)
            {
                productview.gvData.Add(row);
            }
            return true;
        }
    }

Where is your DataGridView object? You need to assign datagridview object's data source to the data table or you can add rows. I answered based on the given details.