0

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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Chen Triều
  • 79
  • 2
  • 6
  • 1
    Where is the `DataGridView` in all this? And is there some reason you are not setting the grids `DataSource` to the `DataTable` returned from the query? From the picture it looks like the code is trying to add a "whole" `DataRow` to the FIRST cell in each row but this is a guess. – JohnG Nov 18 '21 at 06:33
  • I set the data to datagridview in Form code (public DataGridViewRowCollection gvData) – Chen Triều Nov 18 '21 at 06:43
  • and dtgvProduct is the name of my Datagridview – Chen Triều Nov 18 '21 at 06:45
  • In the `ProductPresenter.GetProduct()` method… have you tried to set the grids `DataSource` to the `DataTable` returned from `productGetProductData()`? … Example… instead of looping through the rows of the `DataTable` and adding the rows manually … do something like… `dtgvProduct.DataSource = product.GetProductData();` – JohnG Nov 18 '21 at 06:52
  • Is what I am trying to point out is that your code “appears” to be “manually” adding the rows from the `DataTable` to the grids row collection and this is not necessary. Typically, the code would set the grids `DataSource` and the rows will be created automatically. The same would apply to the columns. If you manually add the columns to the grid, possibly in the designer, then you need to set each columns `DataPropertyName` to match the column name/property name of its `DataSource`. – JohnG Nov 18 '21 at 07:15

1 Answers1

0

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.

shen s
  • 86
  • 7
  • I set the data to datagridview in Form code (public DataGridViewRowCollection gvData) and dtgvProduct is the name of DataGridview. – Chen Triều Nov 18 '21 at 06:56
  • Did you save Data in another data table that was received from the execution of the GetData() function? – shen s Nov 18 '21 at 07:02