배경 스토리: Android용 재고 관리 애플리케이션을 만들고 있습니다(시도 중). 데이터 복제 테스트를 위해 간단한 WinForms 응용 프로그램을 만들었습니다. 이 앱의 목적은 단순히 SQL Server DB(DB 테이블 이름은 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;
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다