Using the RowUpdating event in GridView without an SQL Update command

F

fatmosh

I have a GridView that is pulling data from a SQL database using a
Select command. I would like to use GridView's built-in "Edit, Update,
Cancel" functionality because it is very handy.

However, when the user clicks the Update button I DON'T want to run an
SQL UPDATE command on the database, I just want to handle it myself.

So far to get around this I just create a dummy SQL query that never
updates any rows and let it complete, then do the work I want to do in
RowUpdating.

If I cancel the Update using e.Cancel, the GridView stays in the "Edit"
state, when I want it to go back to the "View" state.

Is there anyway to not specify an UPDATE command at all? If I take it
out it gives me an error.

Any ideas?
 
D

Dr. Know

(e-mail address removed) said:
I have a GridView that is pulling data from a SQL database using a
Select command. I would like to use GridView's built-in "Edit, Update,
Cancel" functionality because it is very handy.

However, when the user clicks the Update button I DON'T want to run an
SQL UPDATE command on the database, I just want to handle it myself.

So far to get around this I just create a dummy SQL query that never
updates any rows and let it complete, then do the work I want to do in
RowUpdating.

If I cancel the Update using e.Cancel, the GridView stays in the "Edit"
state, when I want it to go back to the "View" state.

Is there anyway to not specify an UPDATE command at all? If I take it
out it gives me an error.

Any ideas?

I'll try - I've been messing with ASP.Net2 for a week...
Never even looked at 1.x.
But since I just did a very similar thing... Sure!
In your control declaration:

<asp:GridView ID="GridView1"
runat="server"
DataSourceID="SqlDataSource1"
OnRowUpdating="UpdateCommand" >

In your SQLDataSource:

UpdateCommand=";"

I used the SQL terminator (in JET, anyways...) as the command string.
I believe _some_ string has to be present or the IDE complains?
But since it is either overwritten or never reached, it is irrelevent.

Then add to your code (wherever it may reside...)
Watch the line wrap.

Protected Overridable Sub UpdateCommand(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.GridViewUpdateEventArgs)

Dim selectRow As GridViewRow
Dim _SQLStr As String
selectRow = GridView1.Rows(e.RowIndex)

If createSQLquery

' if you want to generate an ad-hoc query, place your SQL string gen
' code here and continue on, otherwise replace the SQL stuff with your
' code and cancel when finished. Since you've canceled the edit mode
' _request_, it returns to select mode.

_SQLStr = "INSERT INTO yadda... yadda... ;"
SqlDataSource1.UpdateCommand = _SQLStr

Else

' otherwise, do your code and cancel the edit request.

domycode()
e.Cancel = True

End If

End Sub

FWIW,

Greg G.

Dr. Know
 
Joined
May 11, 2006
Messages
2
Reaction score
0
After some googling I've found the following in MSDN :shock: :

You can programmatically put a row in edit mode by setting the EditIndex property with the index of the row. To programmatically exit edit mode, set the EditIndex property to -1.
 
Joined
Aug 29, 2006
Messages
8
Reaction score
0
The correct Way to handle this

The correct way to actually handle this is as follows:

In the RowUpdating event add your code that handles the saving of the changes made to the row.

You should then handle the RowUpdated event for the GridView. In this event you should add the code to return the gridview back to viewing mode (or keep it in edit mode). By default the gridview defaults to staying in edit mode.

The code for this event should look like this:

Code:
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        // Return the Grid View to it's viewing mode
        e.KeepInEditMode = false;
    }

Alternatively, you could also set the GridView's EditIndex property to -1 as stated in the MSDN article previously quoted. However, this must also be done in the RowUpdated event and not the RowUpdating event.

I hope this helps you.

Gary Francis
Software Developer
SSI Computer Services
Providing IT Support services in London
 
Joined
Jun 7, 2007
Messages
1
Reaction score
0
No Row.Updated if e.Cancel = True

Gary Francis said:
The correct way to actually handle this is as follows:

In the RowUpdating event add your code that handles the saving of the changes made to the row.

You should then handle the RowUpdated event for the GridView. In this event you should add the code to return the gridview back to viewing mode (or keep it in edit mode). By default the gridview defaults to staying in edit mode.

The code for this event should look like this:

Code:
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        // Return the Grid View to it's viewing mode
        e.KeepInEditMode = false;
    }

Alternatively, you could also set the GridView's EditIndex property to -1 as stated in the MSDN article previously quoted. However, this must also be done in the RowUpdated event and not the RowUpdating event.

I hope this helps you.

Gary Francis
Software Developer
SSI Computer Services
Providing IT Support services in London

If you set e.Cancel = True (you're going to handle the update yourself) the Updated event doesn't fire.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top