Windows Develop Bookmark and Share   
 index > Windows Forms General > how to edit C# list items?
 

how to edit C# list items?

hey guys,
I'm writin a small application for my uni. project using forms in C#. I've created a stock order processing form, where a person can add, edit & delete stock details such as: stock code, item name, supplier name, unit cost, no. required, & current stock level. Now, I've created a list that allows for me to add a stock detail: public static List<Stock> itemsList = new List<Stock>();

 private void btnOk_Click(object sender, EventArgs e)
        {
            Stock s = new Stock(this.txtStockCode.Text, this.txtItemName.Text, this.txtSupplierName.Text, Double.Parse(txtUnitCost.Text), System.Convert.ToInt32(txtNumberReq.Text), System.Convert.ToInt32(txtCurrStocklvl.Text));
           
            StockItems.itemsList.Add(s);
}

And this works for me just fine. But I haven't able to find anythin useful on the net to help me edit an item from this list. Can someone plz explain how to properly access any added list item so that it appears in the same textboxes I used for adding, and allow me to edit any particular detail (for eg. changing supplier name from walmart to office max) and update that change in the list? Is that even possible?
Any help would be appreciated!!
greatkev33  Wednesday, October 07, 2009 11:15 AM
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();
        }

Malik M.Shahid  Wednesday, October 07, 2009 12:58 PM

Hi greatkev33,

You can also create a BindingSource and set its DataSource property to the stock list. Then bind this BindingSource to the TextBoxes. You can set the Position property of the BindingSource  to move to another Stock object in the list. When the text in the TextBox is changed, the related property of the current object would also be changed. This is a code snippet:
    private List<Stock> _stockList = new List<Stock>();

    private BindingSource _source = new BindingSource();

    private void Form1_Load(object sender, EventArgs e)

    {

        _source.DataSource = _stockList;

        //Bind a text box.

        txtItemName.DataBindings.Add("Text", _source, "ItemName");

        //Bind other textboxes here.

    }

You can get more about how to bind a TextBox to a data source from:
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/eeea922f-846d-4ba1-859d-28d7a9cf7850/

If you also want the text of the textboxes changed when the Stock object changes, you need to have the Stock object implements the INotifyPropertyChanged interface. You can also use
BindingList. These are some documents:
INotifyPropertyChanged:
http://msdn.microsoft.com/en-us/library/ms229614.aspx
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx
BindingList:
http://www.codeproject.com/KB/grid/BindingListExamples.aspx
http://msdn.microsoft.com/en-us/library/ms132679.aspx

Regards,
Aland Li

 


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  4 hours 28 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Mouse Scroll Wheel Input
• Width of Node in owner draw
• ListView ItemActivated Event is Executed Twice !?
• how do i measure the *actual* width of italic styled text?
• Datgrid:Tabindex
• Cursor related with color
• Forms Occasionally Remain Shown After Request To Hide
• hDC -> graphics object
• Application Settings Upgrade
• How to Open excel in C# form