how to get the value of a non key field in RowCommand event?

  • Thread starter Thread starter Ben
  • Start date Start date
B

Ben

Hi,

i know how to get the value of the key field in code-behind, but not how to
get the value of a non key field:

aspx file:
--------
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id"
DataSourceID="SqlDataSource1" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lb1" runat="server" CommandArgument="<%#
Container.DataItemIndex %>" CommandName="Delete">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
....
</asp:GridView>

code-behind:
--------------
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
GridView1.RowCommand

Dim r As Integer = Convert.ToInt32(e.CommandArgument)
Dim dbkey As String
dbkey = GridView1.DataKeys(r).Value
If e.CommandName = "Delete" Then
.....
Me.SqlDataSource1.DeleteCommand = "delete from mytable where id= '"
& dbkey & "'"
......

This works. But how to get the value of a non key field (call it 'myfield')
in order to perform other actions with it.

Thanks
Ben
 
Hi

check out this code

Dim r As Integer = Convert.ToInt32(e.CommandArgument)
Dim dbkey As String
dbkey = GridView1.DataKeys(r).Value
Dim fieldValue As String

'used the r to key row and access the cells

fieldValue = GridView1.Rows(r).Cells(0).Text

Best of luck

Munna
 
Back
Top