views:

1962

answers:

3
+3  Q: 

edit datagrid row

hi...

i want to know how to edit a single row (which i select) from a data grid

for example i have a datagrid with columns A, B and C and i have a couple rows of data, approx 10 rows.

lets say i want to change the value of data within row 4.

how would i do this?

i am using visual studio 2003, but i guess if visual studio 2005 would be okay too. for the coding I'm using c#

thanks..

+1  A: 

Take a look at the documentation for adding an EditItemTemplate to your datagrid. You use the ItemTemplate for view-only, display elements and you use the EditItemTemplate for controls used to bind against a single row that you select.

Here's a link that might help:

http://www.gridviewguy.com/

A: 

Is your data in a DataTable before making it a DataGrid, or can you put it in a DataTable? You can update/delete/edit rows in a DataTable, here's a link with code snippets, pretty straight forward:

http://msdn.microsoft.com/en-us/library/tat996zc(VS.80).aspx

Shahin
+1  A: 

All grid-like components of asp.net have the same machanism as it comes to starting to edit a single row. Actually it's default for asp.net only to edit a single row in a grid.

Needed to start editing is to include asp:button or asp:linkbutton in the ItemTemplate with the CommandName set to "Edit". This one of reserved commandnames all grid-like components knows how to respond to. Clicking this button in DataGrid will raise the EditCommand Event. In this event you have to set the EditItemIndex of the grid equal to Item.Itemindex of the eventargs. This will render the row vaccordeing to the EditItemTemplate.

In this template you put 2 buttons or linkbuttons. One should have the CommandName set to "Update" and one should have the CommandName set to "Cancel".

The "Update" button raises the UpdateCommand event. In which you execute code that store the data in the row to its storage (eg.: database) and sets the EditItemIndex to -1 --> all rows are rendered read-only(ItemTemplate or AlternateItemTemplate).

The "Cancel" button raises the CancelCommand event. In the event handler you have to do si set the EditItemIndex to -1.

This description is only true for DataGrid en not for the in asp.net introduced GridView which handles most of this "Boilerplate"code it self working together with the datasource controls. Google the web for more info on this. it's to much to explain here right now.

Hope it helps?

norbertB