Hi,
Surely Stock code will be Unique code, on the bases of stock code you would be able to Edit, Delete the Stock from StockListItem.
In my thinking , Insert a DataGridView with two Link columns whose name Edit, Delete both of columns property UseColumnTextForLinkValue will be true.
place a Add button on Left top Corner of DataGridView on click of Add add the Stock as you do.
For Edit and Delete operation.
Use the Grid CellContentClick event for these operation.
Declare the StockObject as member variable.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//Edit Mode
if (e.ColumnIndex == 0 && e.RowIndex != -1)
{
StockObject = Form1.StockLits.First(x => x.StockCode == this.dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());
SetValueToControl();
}//Delete Mode
else if (e.ColumnIndex == 1 && e.RowIndex != -1)
{
StockObject = Form1.StockLits.First(x => x.StockCode == this.dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString());
Form1.StockLits.Remove(StockObject);
this.dataGridView1.DataSource = Form1.StockLits.ToList();
}
}
For further create Two method , SetValueToControl() and SetValueToStockList() , on Click of Save button as the assign new value to member Stock object hence it will be update , these method use the following code.
private void SetValueToControl()
{
this.txtStockCode.Text = StockObject.StockCode;
}
private void SetValueToStockList()
{
StockObject.StockCode = this.txtStockCode.Text;
}
On Save Button Click.
private void button2_Click(object sender, EventArgs e)
{
SetValueToStockList();
this.dataGridView1.DataSource = Form1.StockLits.ToList();
}