0

Backstory: I am creating (trying) a inventory management application for android. For data replication testing I made simple WinForms application. Purpose of this app is simply to load data from SQL Server DB (DB table name is T_STOCK). For data manipulation I used Linq and for loading I used DataGridView and for app architecture I am trying to use MVP.

Problem: I am having a hard time to fill DataGrid with data. I already tried to pass hole datagrid as a property and now just a data source. Am I missing something or it should be done in a different way?

This is part of the code that is in my project:

Model:

using System;
using System.Linq;
using System.Configuration;
using InventoryManagment.Services.DE.SQLServer;
using System.Windows.Forms;

namespace InventoryManagment.Models
{
    public class Stock
    {
        private string _ConnectionString;
        object _DgView;

        public object DataGridViewItems { get; set; }

        DEDataContext oDB;
        BindingSource bindingSource;
        DataGridView dataGridView;

        public void GetStockAllRecords()
        {
            _ConnectionString = ConfigurationManager.
                ConnectionStrings["SimpleInventoryManagment_temp." +
                "Properties.Settings.dbInventoryMngConnectionString"].ToString();
            oDB = new Services.DE.SQLServer.DEDataContext(_ConnectionString);
            //int count = (from row in oDB.T_STOCKs select row).Count();

            bindingSource = new BindingSource();
            dataGridView = new DataGridView();
            var stock = from t_stock in oDB.T_STOCKs
                        select new
                        {
                            ID = t_stock.F_ID,
                            Name = t_stock.F_NAME,
                            Barcode = t_stock.F_BARCODE
                        };
            bindingSource.DataSource = stock;
            dataGridView.DataSource = bindingSource;
            _DgView = dataGridView.DataSource;
        }
    }
}

View:

using System;

namespace InventoryManagment.Views
{
    public interface IStock
    {
        object DataGridViewItems { get; set; }
    }
}

Presenter:

using System;
using System.Linq;
using InventoryManagment; 
using System.Windows.Forms;

namespace InventoryManagment.Presenters
{
    public class StockPresenter
    {
        Views.IStock StockView;
        Models.Stock stock = new Models.Stock();

        public StockPresenter(Views.IStock view) { StockView = view; }

        public void GetStockAllRecords()
        {
            stock.GetStockAllRecords();
            StockView.DataGridViewItems = stock.DataGridViewItems;
        }
    }
}

Form

using System;
using System.Windows.Forms;
using InventoryManagment;

namespace InventoryManagment
{
    public partial class FormMain : Form, Views.IStock
    {
        string BtnAddMsgBoxText;

        public FormMain()
        {
            InitializeComponent();
        }

        object Views.IStock.DataGridViewItems
        {
            get
            {
                return GrItems.DataSource;
            }
            set
            {
                GrItems.DataSource = value;
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            Presenters.StockPresenter presenter = 
                new Presenters.StockPresenter(this);
            presenter.GetStockAllRecords();
        }
    }
}

1 Answers1

0

Well I think I was thinking to hard. @Reza Aghaei was right.

public void GetStockAllRecords()
{
    _ConnectionString = ConfigurationManager.
        ConnectionStrings["SimpleInventoryManagment_temp." +
        "Properties.Settings.dbInventoryMngConnectionString"].ToString();
    oDB = new Services.DE.SQLServer.DEDataContext(_ConnectionString);

    var stock = from t_stock in oDB.T_STOCKs
                select new
                {
                    ID = t_stock.F_ID,
                    Name = t_stock.F_NAME,
                    Barcode = t_stock.F_BARCODE
                };
    DataGridViewItems  = stock;
}
  • 1
    If this fixes your issue give it the checkmark so the question doesn't appear open. – Zer0 Jun 25 '18 at 21:09