Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

I have a DataGridView which is bound with the Business object Collection. The Grid populates the Order detail(OrderDetailId,ItemCode, ItemName,Qty,Rate,Amount...) data.

Now I want to edit the item row through grid. The idea is when I click on itemrow in the grid it should fill the row data in the respective controls placed just above the grid(suppose Itemcode cell value in the Itemcode textbox...). after making the necessary changes in the row data user just click on update button and it updates the Grid data even in the Business object collection.

could you please help me what is way to implement this? Do you have any in hand example or you can just give me outline of the logic.

I will appreciate your valuable efforts.

Thanks
Posted
Updated 10-Jun-10 23:17pm
v2

Why haven't you tried accessing collection item bounded to DataGridViewRow using DataBoundItem property.Afterward cast it to the items type and and last edit it and refresh the bindings if needed.
 
Share this answer
 
Comments
rajeshswami27 11-Jun-10 7:37am    
Hi Hristo Bojilov

Thanks for your quick note.

You put me on the right direction.now it is very easy to handle with a particular object from dgview/collection

http://msdn.microsoft.com/en-us/library/4wszzzc7.aspx
Here is a general idea of the logic.

1. when the user clicks the grid (tip: use double-click to avoid accidental triggering when user is just 'looking round') get the DataGridViewRow that was selected, save a reference to a key piece of the data (an identity number or similar), and then pass the row to a method that will:
2. Go through your editing controls one-by-one retrieving the data e.g. myNameTextBox.Text = row.Cells["Name"].Value.ToString()

3. Allow user to edit.
4. When they click the 'Update' button use the key saved earlier to locate the record in your collection and update it e.g. myBusinessObject.Name = myNameTextBox.Text.

What you do next will depend on how your collection is bound to your DataGrid. If you have used a BindingSource (recommended) the grid should update automatically otherwise you will have to re-bind to reflect the changes.

Hope this helps. :)
 
Share this answer
 
Thank you very much Henry

now I have two approaches receiving attention to implement this.

1) Either I retrieve the object through DataBoundItem property for the current row and update the object through the editing control thereafter it will be updated back in the collection as well as in the Grid.

2) Secondly I use the Uniquekey to update the object as you explained.

But I want to know which one perform best when the grid is having a number of records and also heavy business validations implemented in the entity class while user is updating the records.

I think both are perfect :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900