ASPxGridView FAQ: How to Enable Editing

30 August 2007

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?

 

.

9 comment(s)
Fatih Hayrio??lu’nun not defteri » 01 Eyl??l 2007 Web’den Se??me Haberler

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

31 August, 2007
Mourad

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

14 August, 2008
Mehul Harry (DevExpress)

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.

14 August, 2008
Adam Mork

How do you do this strictly in C# ?

Im using a DataTable for my datasource

12 October, 2009
Mehul Harry (DevExpress)

Adam,

Check out these resources:

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

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

13 October, 2009
Fadi Al-Damiri

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

29 October, 2010
Mehul Harry (DevExpress)

Fadi,

Try the approach here:

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

The may also help:

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

29 October, 2010
Felipe R Machado

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.

13 June, 2011
Mehul Harry (DevExpress)

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.

13 June, 2011

Please login or register to post comments.