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
 

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

Top