how to add edit column in datagrid?

actually I have add the Edit buttonlink in the datagrid using the datagrid properties diaolog box ..
so, what are the next steps? so that I could use the Edit link to edit the entire row of data..
thank you.
p931226Asked:
Who is Participating?
 
mmarinovConnect With a Mentor Commented:
Hi,

you have to create an edit command event like this

vb
      Sub ItemsGrid_Edit(sender As Object, e As DataGridCommandEventArgs)

         ' Set the EditItemIndex property to the index of the item clicked
         ' in the DataGrid control to enable editing for that item. Be sure
         ' to rebind the DateGrid to the data source to refresh the control.
         ItemsGrid.EditItemIndex = e.Item.ItemIndex
         BindGrid()

      End Sub
      Sub BindGrid()

         ' Set the data source and bind to the Data Grid control.
         ItemsGrid.DataSource = CartView
         ItemsGrid.DataBind()

      End Sub


c#
      void ItemsGrid_Edit(Object sender, DataGridCommandEventArgs e)
      {

         // Set the EditItemIndex property to the index of the item clicked
         // in the DataGrid control to enable editing for that item. Be sure
         // to rebind the DateGrid to the data source to refresh the control.
         ItemsGrid.EditItemIndex = e.Item.ItemIndex;
         BindGrid();

      }
      void BindGrid()
      {

         // Set the data source and bind to the Data Grid control.
         ItemsGrid.DataSource = CartView;
         ItemsGrid.DataBind();

      }

and in your html tag asp:datagrid you have to add
OnEditCommand="ItemsGrid_Edit"


Regards,
B..M
0
 
p931226Author Commented:
Hi mmarinov,

I have used ur code given..
but stil can't edit the entire row?
when I click on the Edit link, it has no reaction??
Why can't it change to a row that is editable?
TQ
0
 
p931226Author Commented:
when I put OnEditCommand="myGrid_Edit" inside the html tag asp:datagrid
It has this error:
-----------------------------------------------------------------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0122: 'Las.UserControl.myGrid_Edit(object, System.Web.UI.WebControls.DataGridCommandEventArgs)' is inaccessible due to its protection level
--------------------------------------------------------------------------------------------
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
mangesh004Connect With a Mentor Commented:
Hi friend,

If you are using VB.NET and VB code behind files to write your code, then follow these steps

1. open that file (for example sample.aspx.vb)
2.you can see two dropdown lists at the top
3. from first choose your data grid name (known as class name)
4.from the second dropdownlist choose method name called as EditCommand
5. when you do this you can see that a new procedure  is created , as follows

Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
   
End Sub

6.write the following code into it

DataGrid1.EditItemIndex = e.Item.ItemIndex
fill_datagrid()  ‘ your procedure which binds the grid with Data

7.things to be taken care of
   
a) if you are populating the grid in the page load event itself put that code into if loop as
If IsPostBack = False Then
            fill_datagrid()
End If

b)see to it that you have not set the properties of the column to Read only
-------- It should definetly work

Thanking you
Mangesh
0
 
mmarinovCommented:
you have to crea the event of your datagrid:
1. first way is by clicking on the datargid control in the design mode , select the events and double click on the ItemEdit command
2. the second way is to put public before your event definition so the asp.net process to know which sub have to execute when the edit command event is rised

if you don't have on of these scenarious you will not be switched to the edit mode of the selected row

B..M
0
 
p931226Author Commented:
I'm using C#

Whenever I removed the OnEditCommand="myGrid_Edit" from the html tag..
it has no error..
but when I click on Edit link..
it will change to Update and Cancel..
what to put inside the

            void myGrid_Cancel(Object sender, DataGridCommandEventArgs e)
            {


                  myGrid.EditItemIndex = -1;
                  BindGrid();
                  
            }
 
            void myGrid_Update(Object sender, DataGridCommandEventArgs e)
            {
                  
            
            }
 
=====================
I have put some codes in the myGrid_Cancel function..
but seems like no respond...
is it bcos of I din't put the
OnCancelCommand in the html tag?

0
 
mmarinovCommented:
the code in onCancelCommand event seems to be ok - it should return in preview mode,
in the update mode you have to get the values of the editable boxes and update them in the database

to see a full example see here :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatagridclasseditcommandtopic.asp

B..M
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.