DataKey Question

G

Guest

When deleting in a Datagrid, I'm using a Datakey to reference the deletion
since the specific table I'm deleting from doesn't have a index or ID for
each entry.

Sub myDataGrid_Delete(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Dim SelectedRow As String = myDataGrid.DataKeys(e.Item.ItemIndex)

'TODO: Delete the record from the database
Dim strSQL As String = _
"DELETE FROM myTable" _
& "WHERE(my_column_name = " & SelectedRow & ")"
'Rebind the DataGrid
myDataGrid.DataBind()

End Sub

and in my datagrid declaration I've got [...DataKeyField="my_column_name"].
How do I reference the actual text in that cell for the delete command? ANY
help would be appreciated.
 
K

Karl Seguin

well, you can access values via:
e.Item.Cell(X).Text
where X is the index of the cell or if you are using templateColumns, you
can place it in a literal control and do something like

ctype(e.Item.FindControl("LITERAL_ID"), Literal).Text

Karl
 
G

Guest

I'm using BoundColumns so I don't have an ID for the column and
e.Item.Cells(x).Text seems like it would work but still doesn't.

I've changed my method to:

Sub myDataGrid_Delete(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Dim SelectedRow As String = e.Item.Cells(6).Text

'TODO: Delete the record from the database
Dim strSQL As String = _
"DELETE FROM myTable" _
& "WHERE(my_column_name = " & SelectedRow & ")"
'Rebind the DataGrid
myDataGrid.DataBind()
End Sub

I'm using Cells(6) since the 7th column in the datagrid contains the data I
want to use as an identifier for the SQL delete statement.



Karl Seguin said:
well, you can access values via:
e.Item.Cell(X).Text
where X is the index of the cell or if you are using templateColumns, you
can place it in a literal control and do something like

ctype(e.Item.FindControl("LITERAL_ID"), Literal).Text

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!



Eustice Scrubb said:
When deleting in a Datagrid, I'm using a Datakey to reference the deletion
since the specific table I'm deleting from doesn't have a index or ID for
each entry.

Sub myDataGrid_Delete(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Dim SelectedRow As String = myDataGrid.DataKeys(e.Item.ItemIndex)

'TODO: Delete the record from the database
Dim strSQL As String = _
"DELETE FROM myTable" _
& "WHERE(my_column_name = " & SelectedRow & ")"
'Rebind the DataGrid
myDataGrid.DataBind()

End Sub

and in my datagrid declaration I've got
[...DataKeyField="my_column_name"].
How do I reference the actual text in that cell for the delete command?
ANY
help would be appreciated.
 
K

Karl Seguin

If Cells(7) is a BoundCOlumn, the Text property should return the data
within the cell. You might want to debug/breakpoint and see exactly what
Cells(6) is. It's possible the value you are looking for is stored within a
control. For example, if the column was a ButtomColumn, you'd need to do:

ctype(Cells(X).Controls(0), DataGridLinkButton).Text

Having said all of this, why can't you use the DataKey directly? Or why
can't use create an id column? (what happens if 2 records have the same
"Text" value? the delete will delete all matching records...)

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
Eustice Scrubb said:
I'm using BoundColumns so I don't have an ID for the column and
e.Item.Cells(x).Text seems like it would work but still doesn't.

I've changed my method to:

Sub myDataGrid_Delete(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Dim SelectedRow As String = e.Item.Cells(6).Text

'TODO: Delete the record from the database
Dim strSQL As String = _
"DELETE FROM myTable" _
& "WHERE(my_column_name = " & SelectedRow & ")"
'Rebind the DataGrid
myDataGrid.DataBind()
End Sub

I'm using Cells(6) since the 7th column in the datagrid contains the data
I
want to use as an identifier for the SQL delete statement.



Karl Seguin said:
well, you can access values via:
e.Item.Cell(X).Text
where X is the index of the cell or if you are using templateColumns,
you
can place it in a literal control and do something like

ctype(e.Item.FindControl("LITERAL_ID"), Literal).Text

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!



message
When deleting in a Datagrid, I'm using a Datakey to reference the
deletion
since the specific table I'm deleting from doesn't have a index or ID
for
each entry.

Sub myDataGrid_Delete(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Dim SelectedRow As String =
myDataGrid.DataKeys(e.Item.ItemIndex)

'TODO: Delete the record from the database
Dim strSQL As String = _
"DELETE FROM myTable" _
& "WHERE(my_column_name = " & SelectedRow & ")"
'Rebind the DataGrid
myDataGrid.DataBind()

End Sub

and in my datagrid declaration I've got
[...DataKeyField="my_column_name"].
How do I reference the actual text in that cell for the delete command?
ANY
help would be appreciated.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top