title: The MVP Pattern with presentation model
body: <p>I have some questions about the MVC pattern and i am looking for feedback for what i have made.</p>
  
  <p>After watching a course about the MVP pattern i started to rebuild an excercise program to this pattern. I came across some problems and question while i was at it.</p>
  
  <ol>
  <li><p>Why do people use the interface in the pattern? I saw it sometimes in the MVC pattern and felt it wasn't always necessary but it seems almost anyone uses it.</p></li>
  <li><p>How exactly do i fill the DataGridView in this case? </p></li>
  </ol>
  
  <p>And this is what i made:</p>
  
  <p>Model:</p>
  
  <pre><code>  public class PrinterModel
  {
  
      public string printerSearch;
      public string printerCode;
      public string printer;
      public string printerType;
      public DataTable printers;
  
      public PrinterModel()
      {
      }
  }
  </code></pre>
  
  <p>Query Class:</p>
  
  <pre><code> 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;
      }
  }
  </code></pre>
  
  <p>CopyPrinterView:</p>
  
  <pre><code> 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; }
      }
  
  }
  </code></pre>
  
  <p>SearchView:</p>
  
  <pre><code> 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; }
      }
  }
  </code></pre>
  
  <p>CopyPrinterPresenter:</p>
  
  <pre><code> 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);
  
      }
  }
  </code></pre>
  
  <p>SearchPresenter:</p>
  
  <pre><code> 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);
          }
      }
  }
  </code></pre>
  
  <p>MainForm:</p>
  
  <pre><code> 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)
      {
  
      }
  
  }
  </code></pre>