Solved

how to add edit column in datagrid?

Posted on 2004-09-13
7
382 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:p931226
  • 3
  • 3
7 Comments
 
LVL 28

Accepted Solution

by:
mmarinov earned 50 total points
ID: 12042903
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
 

Author Comment

by:p931226
ID: 12050267
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
 

Author Comment

by:p931226
ID: 12050307
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Assisted Solution

by:mangesh004
mangesh004 earned 20 total points
ID: 12051977
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12052713
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
 

Author Comment

by:p931226
ID: 12053888
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
 
LVL 28

Expert Comment

by:mmarinov
ID: 12054177
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now