c# паттерн MVP управление формами через MainView
при отображение формы с помощью главной формы MainView не могу отобразить данные правильно, а так же не могу самостоятельно настроить ширину Datagridview, выдает исключение, помогите пожалуйста разобраться как управлять формами с помощью главной формы по паттерну MVP, и отображать данные правильно.
форма должна выглядит так :
выглядит так :

ниже выложил код и изображение ошибки
namespace Telecom_Sklad_MVP.Views
{
public partial class MainView : Form, IMainView
{
public MainView()
{
InitializeComponent();
btn_Product.Click += delegate { ShowProductView?.Invoke(this, EventArgs.Empty); };
}
public event EventHandler ShowProductView;
private void MainView_Load(object sender, EventArgs e)
{
}
public DataGridView DVG()
{
throw new NotImplementedException();
}
}
}
код MainPresenter
namespace Telecom_Sklad_MVP.Preseneters
{
public class MainPresenter
{
private IMainView mainView;
private readonly string mysqlConnectionString;
public MainPresenter(IMainView mainView, string mysqlConnectionString)
{
this.mainView = mainView;
this.mysqlConnectionString = mysqlConnectionString;
this.mainView.ShowProductView += ShowProductView;
}
private void ShowProductView(object sender, EventArgs e)
{
IProductView view = ProductView.GetInstance((MainView)mainView);
IProductRepository repository = new ProductRepository(mysqlConnectionString);
new ProductPresenter(view, repository);
}
}
}
главный интерфейс
namespace Telecom_Sklad_MVP.Views
{
public interface IMainView
{
event EventHandler ShowProductView;
DataGridView DVG();
}
}
namespace Telecom_Sklad_MVP.Views
{
public partial class ProductView : Form, IProductView
{
private string message;
private bool isSuccessful;
private bool isEdit;
public ProductView()
{
InitializeComponent();
AssociateAndRaiseViewEvents();
}
public string ProductId
{
get { return txt_Id.Text; }
set { txt_Id.Text = value; }
}
public string ProducttName
{
get { return txt_productName.Text; }
set { txt_productName.Text = value; }
}
public string ProductQty
{
get { return txt_qty.Text; }
set { txt_qty.Text = value; }
}
public string ProductPrice
{
get { return txt_price.Text; }
set { txt_price.Text = value; }
}
public string ProductTotal
{
get { return txt_total.Text; }
set { txt_total.Text = value; }
}
public string ProductNote
{
get { return txt_note.Text; }
set { txt_note.Text = value; }
}
public string ProductDate
{
get { return dateTimePicker1.Text; }
set { dateTimePicker1.Text = value; }
}
public string CategoryId
{
get { return labelIdCategory.Text; }
set { labelIdCategory.Text = value; }
}
public string CompanyId
{
get { return comboBox_Company.Text; }
set { comboBox_Company.Text = value; }
}
public string UnitId
{
get { return comboBox_unit.Text; }
set { comboBox_unit.Text = value; }
}
public bool IsEdit
{
get { return isEdit; }
set { isEdit = value; }
}
public bool IsSuccessful
{
get { return isSuccessful; }
set { isSuccessful = value; }
}
public string Message
{
get { return message; }
set { message = value; }
}
public string ComboboxCategoryId
{
get { return comboBox_Category.Text; }
set { comboBox_Category.Text = value; }
}
public string ComboboxCategoryName
{
get { return comboBox_Category.Text; }
set { comboBox_Category.Text = value; }
}
public event EventHandler AddNewEvent;
public event EventHandler EditEvent;
public event EventHandler DeleteEvent;
public event EventHandler SaveEvent;
public void SetProductListBindingSource(BindingSource productList)
{
dataGridView1.DataSource = productList;
}
private void AssociateAndRaiseViewEvents() // вызов событий или еще что-то
{
// Add new
btn_Add.Click += delegate
{
AddNewEvent?.Invoke(this, EventArgs.Empty);
};
// Edit
btn_Edit.Click += delegate
{
EditEvent?.Invoke(this, EventArgs.Empty);
};
// Save
btn_Add.Click += delegate
{
SaveEvent?.Invoke(this, EventArgs.Empty);
if (isSuccessful)
{
MessageBox.Show(Message);
}
};
// Delete
btn_delete.Click += delegate
{
var result = MessageBox.Show("Вы хотите удалить выделенную строку?", "Warning",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
DeleteEvent?.Invoke(this, EventArgs.Empty);
MessageBox.Show(Message);
}
};
// DVGStyle();
}
private void ProductView_Load(object sender, EventArgs e)
{
comboBox_Category.DisplayMember = "name";
comboBox_Category.ValueMember = "id";
comboBox_Category.Text = "Select Category";
comboBox_Company.DisplayMember = "name";
comboBox_Company.ValueMember = "id";
comboBox_Company.Text = "Select Company";
comboBox_unit.DisplayMember = "name";
comboBox_unit.ValueMember = "id";
comboBox_unit.Text = "Select Unit";
// DVGStyle();
DVGStyle();
}
public void DVGStyle()
{
dataGridView1.Columns[0].Width = 50;
dataGridView1.Columns[1].Width = 150;
dataGridView1.Columns[2].Width = 60;
dataGridView1.Columns[3].Width = 70;
dataGridView1.Columns[4].Width = 80;
dataGridView1.Columns[5].Width = 80;
dataGridView1.Columns[6].Width = 120;
dataGridView1.Columns[7].Width = 120;
dataGridView1.Columns[8].Width = 120;
dataGridView1.Columns[9].Width = 120;
}
void IProductView.ShowDialog()
{
comboBox_Category.DisplayMember = "name";
comboBox_Category.ValueMember = "id";
comboBox_Category.Text = "Saýla";
}
private static ProductView instance;
public static ProductView GetInstance(Form parentContainer)
{
if (instance == null || instance.IsDisposed)
{
instance = new ProductView
{
MdiParent = parentContainer,
FormBorderStyle = FormBorderStyle.None,
Dock = DockStyle.Fill
};
}
else
{
if (instance.WindowState == FormWindowState.Minimized)
instance.WindowState = FormWindowState.Normal;
instance.BringToFront();
}
return instance;
}
public void SetCategoryListBindingSource(BindingSource categoryList)
{
comboBox_Category.DataSource = categoryList;
}
public void SetCompanyListBindingSource(BindingSource companyList)
{
comboBox_Company.DataSource = companyList;
}
public void SetUnitListBindingSource(BindingSource unitList)
{
comboBox_unit.DataSource = unitList;
}
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
txt_Id.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
txt_productName.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
txt_qty.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
comboBox_unit.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
txt_price.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
txt_total.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
comboBox_Category.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
comboBox_Company.Text = dataGridView1.CurrentRow.Cells[7].Value.ToString();
txt_note.Text = dataGridView1.CurrentRow.Cells[8].Value.ToString();
}
private void Txt_qty_TextChanged(object sender, EventArgs e)
{
try
{
if (txt_qty.Text == "")
{
txt_total.Clear();
}
else if (txt_productName.Text == "")
{
MessageBox.Show("Please , Enter Product Name ", "Try Again", MessageBoxButtons.OK, MessageBoxIcon.Error);
txt_productName.Focus();
}
else
{
double tprice;
tprice = double.Parse(txt_price.Text) * double.Parse(txt_qty.Text);
txt_total.Text = tprice.ToString();
}
}
catch (Exception)
{
}
}
private void ComboBox_Category_SelectedIndexChanged(object sender, EventArgs e)
{
labelIdCategory.Text = comboBox_Category.SelectedValue.ToString();
}
private void ComboBox_unit_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void SetUnitListBindingSource(BindingSource unitList, ComboBox unit)
{
comboBox_unit.DataSource = unitList;
}
}
}