我对 MVC 模式有一些疑问,我正在寻找我所做的反馈。
在观看了关于 MVP 模式的课程后,我开始为这个模式重建一个练习程序。我在做的时候遇到了一些问题和疑问。
人们为什么要使用模式中的接口?我有时在 MVC 模式中看到它,并觉得它并不总是必要的,但似乎几乎所有人都在使用它。
在这种情况下,我究竟如何填充 DataGridView?
这就是我所做的:
模型:
public class PrinterModel
{
public string printerSearch;
public string printerCode;
public string printer;
public string printerType;
public DataTable printers;
public PrinterModel()
{
}
}
查询类:
public class Querys
{
DataTable dtbl = new DataTable();
string connectionString = @"CORRECT CONNECTION STRING";
//Fills the DataTable with data
public DataTable DBFillTable()
{
using (MySqlConnection sqlCon = new MySqlConnection(connectionString))
{
MySqlCommand cmd = new MySqlCommand("SELECT * FROM PrinterTypes", sqlCon);
MySqlDataAdapter sqlDa = new MySqlDataAdapter(cmd);
sqlDa.Fill(dtbl);
return dtbl;
}
}
// Searches for printer Type
public DataTable SearchPrinterType(string search)
{
DataTable dtbl2 = new DataTable();
if (search != "")
{
try
{
dtbl2 = dtbl.Select("PrinterType LIKE '%" + search + "%'").CopyToDataTable();
}
catch (Exception ex)
{
}
}
else if (search == "")
{
dtbl2 = dtbl;
}
return dtbl2;
}
/* Searches for the Printer Code. */
public string SearchPrinterCode(string search)
{
string result = null;
try
{
var filteredRow = dtbl.Select("PrinterCode = '" + search + "'");
result = filteredRow[0][0].ToString() + " " + filteredRow[0][1].ToString();
}
catch (Exception ex)
{
}
return result;
}
}
复制打印机视图:
public partial class CopyPrinterView : MetroUserControl
{
private PrinterModel printer;
public CopyPrinterView()
{
InitializeComponent();
}
public string PrinterSearch
{
get { return TxtSearchResult.Text; }
set { TxtSearchResult.Text = value; }
}
public string PrinterCode
{
get { return TxtPrinterCode.Text; }
set { TxtPrinterCode.Text = value; }
}
public event EventHandler CopyPrinter
{
add { BtnCopyPrinterCode.Click += value; }
remove { BtnCopyPrinterCode.Click -= value; }
}
public event EventHandler TxtPrinterCodeChanged
{
add { TxtPrinterCode.TextChanged += value; }
remove { TxtPrinterCode.TextChanged -= value; }
}
}
搜索视图:
public partial class SearchView : MetroUserControl
{
private PrinterModel printer;
public SearchView()
{
InitializeComponent();
}
public string PrinterType
{
get { return TxtPrinterType.Text; }
set { TxtPrinterType.Text = value; }
}
public event DataGridViewCellEventHandler DvgDoubleClick
{
add { DvgPrinters.CellDoubleClick += value; }
remove { DvgPrinters.CellDoubleClick -= value; }
}
public event EventHandler TxtPrinterTypeResultChanged
{
add { TxtPrinterType.TextChanged += value; }
remove { TxtPrinterType.TextChanged -= value; }
}
public event EventHandler LoadDvg
{
add { Load += value; }
remove { Load -= value; }
}
}
CopyPrinter 演示者:
public class CopyPrinterPresenter
{
private CopyPrinterView copyPrinterView;
private PrinterModel printer;
private Querys query;
public CopyPrinterPresenter(CopyPrinterView copyPrinterView, PrinterModel printer, Querys query)
{
this.copyPrinterView = copyPrinterView;
this.printer = printer;
this.query = query;
copyPrinterView.TxtPrinterCodeChanged += TxtPrinterCode_TextChanged;
copyPrinterView.CopyPrinter += BtnCopyPrinterCode_Click;
copyPrinterView.PrinterCode += printer.printerCode;
copyPrinterView.PrinterSearch += printer.printerSearch;
}
private void BtnCopyPrinterCode_Click(object sender, EventArgs e)
{
if (printer.printerSearch != "")
{
// Copy printer to clipboard if there is a result in TxtSearchResult.Text
Clipboard.SetText(printer.printerSearch);
string message = "Copied the printer of printer code: " + printer.printerCode;
MetroMessageBox.Show(copyPrinterView, message, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
string message = "Fill in a Printer Code";
MetroMessageBox.Show(copyPrinterView, message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void TxtPrinterCode_TextChanged(object sender, EventArgs e)
{
printer.printerSearch = query.SearchPrinterCode(printer.printerCode);
}
}
搜索主持人:
public class SearchPresenter
{
private SearchView searchView;
private PrinterModel printer;
private Querys query;
public SearchPresenter(SearchView searchView, PrinterModel printer, Querys query)
{
this.searchView = searchView;
this.printer = printer;
this.query = query;
searchView.Load += SearchView_Load;
searchView.TxtPrinterTypeResultChanged += TxtPrinterType_TextChanged;
searchView.DvgDoubleClick += DvgPrinters_CellDoubleClick;
searchView.PrinterType += printer.printerType;
}
private void SearchView_Load(object sender, EventArgs e)
{
//Initial load
printer.printers = query.DBFillTable();
searchView.DvgPrinters.DataSource = printer.printers;
}
private void TxtPrinterType_TextChanged(object sender, EventArgs e)
{
//Displays result of the query into the DataGridView
printer.printers = query.SearchPrinterType(printer.printerType);
}
private void DvgPrinters_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
// Copies the printercode to the clipboard of the selected cell in the row.
if (printer.printers.Rows[e.RowIndex] != null)
{
//printer.printers.Rows[e.RowIndex].Cells["PrinterCode"].Value.ToString()
Clipboard.SetText(printer.printers.Rows[e.RowIndex][2].ToString());
string message = "Copied the Printer Code of the selected row";
MessageBox.Show(message);
}
}
}
主窗体:
public partial class MainMetroFormCopy : MetroForm
{
Querys query = new Querys();
PrinterModel printer = new PrinterModel();
public MainMetroFormCopy()
{
InitializeComponent();
var searchViewPresenter = new SearchPresenter(searchView1,printer, query);
var copyPrinterViewPresenter = new CopyPrinterPresenter(copyPrinterView1,printer ,query);
}
private void MainMetroForm_Load(object sender, EventArgs e)
{
}
}