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

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
 
M

Munna

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
 

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,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top