如何在 MVP 项目中填充 DataGridView?
How to fill DataGridView in a MVP project?
背景故事:我正在为 android 创建(尝试)库存管理应用程序。对于数据复制测试,我制作了简单的 WinForms 应用程序。此应用程序的目的只是从 SQL 服务器数据库(数据库 table 名称为 T_STOCK)加载数据。对于数据操作,我使用了 Linq,对于加载,我使用了 DataGridView,对于应用程序架构,我尝试使用 MVP。
问题:我很难用数据填充 DataGrid。我已经尝试将孔数据网格作为 属性 传递,现在只是一个数据源。我是不是遗漏了什么或者应该以不同的方式完成?
这是我项目中的部分代码:
型号:
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;
}
}
}
查看:
using System;
namespace InventoryManagment.Views
{
public interface IStock
{
object DataGridViewItems { get; set; }
}
}
主持人:
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;
}
}
}
表格
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();
}
}
}
好吧,我想我是在努力思考。 @Reza Aghaei 是对的。
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;
}
背景故事:我正在为 android 创建(尝试)库存管理应用程序。对于数据复制测试,我制作了简单的 WinForms 应用程序。此应用程序的目的只是从 SQL 服务器数据库(数据库 table 名称为 T_STOCK)加载数据。对于数据操作,我使用了 Linq,对于加载,我使用了 DataGridView,对于应用程序架构,我尝试使用 MVP。
问题:我很难用数据填充 DataGrid。我已经尝试将孔数据网格作为 属性 传递,现在只是一个数据源。我是不是遗漏了什么或者应该以不同的方式完成?
这是我项目中的部分代码:
型号:
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;
}
}
}
查看:
using System;
namespace InventoryManagment.Views
{
public interface IStock
{
object DataGridViewItems { get; set; }
}
}
主持人:
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;
}
}
}
表格
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();
}
}
}
好吧,我想我是在努力思考。 @Reza Aghaei 是对的。
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;
}