DataGrid EventCommands? I just can't get it...

S

SSP

I just can't seem to get the DeleteCommand and UpdateCommand work in a
datagrid.

My setup includes Visual Studio.NET 2003, C# Web Application.
I can very easily create the oleDbConnection, oleDbDataAdapter, the Dataset
and bind to the DataGrid.
I just don't know what to write to be able to Update and Delete tables.

private void DataGrid2_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
????
}

:-(
SSP
 
S

Stevie_mac

I normally do this...
* Add Edit / Update / Cancel / Delete columns (as required)
* Get an OleDbDataReader from my source data
* Bind the DataReader to DataGrid (Data readers are Readonly Forward Only &
therefore faster & much more lightweight)
* On the DeleteCommand, i use information from the columns to build a SQL string
or Command object
* Call ExecuteNonQuery on the OleDbCommand

example
Table has 3 cols... DELETE | Field1 | Field2

Use this code to delete the current row (untested & no error trapping mind!!!)
Dim sCell1Text As String = e.Item.Cells(1).Text
Dim sCell2Text As String = e.Item.Cells(2).Text
Dim conn As New OleDb.OleDbConnection("User id=test; blah blah")
conn.Open
Dim sSQL As String = "DELETE FROM MyTable "
sSQL &= "WHERE Field1='" & sCell1Text & "'"
sSQL &= " AND Field2='" & sCell2Text & "';"
Dim o As New OleDb.OleDbCommand(sSQL, conn)
o.ExecuteNonQuery()

(yes i know its not C# but you can see whats going on - i'll convert it to c# if
you don't know VB!)

NOTE - a common mistake is trying to capture Delete event in DeleteCommand (and
update / cancel / edit) after convering the Delete column to a TEMPLATE item -
Templated items events are raised in DataGrid_ItemCommand - you distinguish them
by e.CommandName
 

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
474,137
Messages
2,570,797
Members
47,345
Latest member
tektheone

Latest Threads

Top