Blogs

News

Email Subscriptions

Mehul Harry's DevExpress Blog

ASPxGridView FAQ: How to Enable Editing

     

Want to enable your ASPxGridView to edit data? It's very easy and similar to the GridView method. Let me show you how:

To enable editing, simply set the 'Enable Editing' checkbox from the smart tag:

image  

Be sure to set the DataSource to support editing by defining the InsertCommand, UpdateCommand, and DeleteCommand. There is a easy way to do this by simply configuring the DataSource:

image

Now after you define the connection and your SelectCommand then you have the opportunity to allow the DataSource to generate the other command statements for you. Simply click on the Advanced Button when you're on the 'Configure the Select Statement' dialog. Make sure the checkbox labeled 'Generate INSERT, UPDATE, and DELETE statements' checkbox is checked and then simply click 'OK'.

image

Now you're DataSource should look something like this:

 

<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
    DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = ?" InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [City], [Country]) VALUES (?, ?, ?, ?)"
    SelectCommand="SELECT [CustomerID], [CompanyName], [City], [Country] FROM [Customers]"
    UpdateCommand="UPDATE [Customers] SET [CompanyName] = ?, [City] = ?, [Country] = ? WHERE [CustomerID] = ?">
    <DeleteParameters>
        <asp:Parameter Name="CustomerID" Type="String" />
    </DeleteParameters>
    <UpdateParameters>
        <asp:Parameter Name="CompanyName" Type="String" />
        <asp:Parameter Name="City" Type="String" />
        <asp:Parameter Name="Country" Type="String" />
        <asp:Parameter Name="CustomerID" Type="String" />
    </UpdateParameters>
    <InsertParameters>
        <asp:Parameter Name="CustomerID" Type="String" />
        <asp:Parameter Name="CompanyName" Type="String" />
        <asp:Parameter Name="City" Type="String" />
        <asp:Parameter Name="Country" Type="String" />
    </InsertParameters>
</asp:AccessDataSource>

 

You're now ready to start editing, inserting and deleting records within the ASPxGridView.

Note: If you've enabled editing in the ASPxGridView but your DataSource doesn't contain these commands then you'll likely see an error like this when trying to edit:

image

To remove this error message you can:

  1. Create your own customer error message
  2. Suppress the error message

In both cases you'll want to intercept the data changing event as soon as the button is clicked. For example, to intercept before the row is updated simply override the ASPxGridView.RowUpdating method. For the other edit events, please use these corresponding methods: RowInserting, RowDeleting, and RowValidating.

So to create your own error message simply throw an exception from the event like so:

 

protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
    throw new InvalidOperationException("Data modifications are not allowed");
}

 

To suppress the message simply call the Cancel method like so:

 

protected void grid_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
{
    e.Cancel = true;
}

 

 

Do you need to manually update your own data without going through the DataSource?

Then you'll need to use the RowUpdating method again. This tutorial give a good example of how to implement this in your own code:

Customize data updates using the RowUpdating event

 

You may find this tutorial helpful if you want define your own buttons for editing or enable editing by double-clicking the row:

Switch to the edit mode by clicking a status bar button or by double-clicking a row

 

So do you feel like an editing guru yet?

 

.

Published Aug 30 2007, 01:00 AM by Mehul Harry (DevExpress)
Filed under: ,
Technorati tags: ASP.NET, ASPxGridView
Bookmark and Share

Comments

 

Fatih Hayrio??lu’nun not defteri » 01 Eyl??l 2007 Web’den Se??me Haberler said:

Pingback from  Fatih Hayrio??lu&#8217;nun not defteri &raquo; 01 Eyl??l 2007 Web&#8217;den Se??me Haberler

August 31, 2007 5:52 PM
 

Mourad said:

How to cancel Edit Mode without a callback to the server, I want it to have a good performance of the grid.

Or how I can move from one edit row to another without callback to the server

August 14, 2008 10:56 AM
 

Mehul Harry (DevExpress) said:

Hi Mourad,

The ASPxGridView uses callbacks for setting/canceling the editrows. I don't believe it's possible to have it completely on the clientside. However, if there is a reason you need this then I recommend creating a suggestion in the support center. Thanks.

August 14, 2008 2:44 PM
 

Adam Mork said:

How do you do this strictly in C# ?

Im using a DataTable for my datasource

October 12, 2009 9:16 PM
 

Mehul Harry (DevExpress) said:

Adam,

Check out these resources:

www.devexpress.com/.../E257.aspx

search.devexpress.com|P5|57&d=16

October 13, 2009 11:18 AM
 

Fadi Al-Damiri said:

How can you do this at runtime with dataset in asp.net with vb code?

October 29, 2010 6:09 PM
 

Mehul Harry (DevExpress) said:

Fadi,

Try the approach here:

www.devexpress.com/.../E257.aspx

The may also help:

search.devexpress.com|P5|57&d=16

October 29, 2010 6:21 PM
 

Felipe R Machado said:

Oh! I thought that this article might have been the answer I was looking for but it seems that it is only aimed towards newbies... I need to allow or prevent editing at runtime, not using a design-time feature like this... I've tried to see if the grid (like the GridView) had a settings to control if it can Edit, Insert, Update, Delete, etc. But I've found none... It seems that I'll have to try to hide the command buttons themselves.. Overly complicated for such a "simple" task.

June 13, 2011 1:22 PM
 

Mehul Harry (DevExpress) said:

Felipe,

Because you'll need to define custom logic as when an editor linkbutton should not be displayed, we don't add it as a standard feature, sorry.

Try some of the approaches mentioned here:

http://dxpr.es/lPu6Tv

Thanks.

June 13, 2011 3:55 PM

About Mehul Harry (DevExpress)

Mehul Harry is an ASP.NET technical evangelist at Developer Express. You can reach him directly at mharry@DevExpress.com. You can also follow him on Twitter: http://twitter.com/mehulharry
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.