Advertisement

04.26.2008 at 04:44AM PDT, ID: 23355713
[x]
Attachment Details

How to get the selected row or rowindex in a GridView when there's no CommandField?

Asked by cdebel in Programming for ASP.NET, .NET, Microsoft Visual Basic.Net

Tags:

Here's my problem.  I've a GridView with several buttons in it.  Each button have a CommandName.
I've discovered that when i click on a button in a row, it doesn't select the row and by mather of fact doesn't update the DataKeys.

Is there a way to know on which ROW the button was pressed?  (So this way i could get all the data in other columns of this row).

I thought about adding a column containing "iKey" but make it "invisible" like this:
<asp:BoundField DataField="iKey" Visible="false" />

but in my codebehind when i try to read the value of that invisible cell, it return an empty string.  If i was able to get that key, i could at least do something like this...
for each oRow in gvSubscribedServices.Row
      ' if first column = ikService then
      '     THIS IS MY ROW
      ' endif
next

I'm sure i'm doing things more complicated because there's something i didn't understood yet, so if you have any idea, i would like that.

In the worse case scenario, i'll simply add some code in my ActualSvcCmd sub to connect to the DB, use an SQLDataReader and read all the fields that i need.  It's gonna be slower, and since the data is already loaded in that page, i think there's a way to find that row.

Thanks
----------------------------------------------------------------------------------------------------
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
<asp:GridView id="gvSubscribedServices" runat="server 
AutoGeneratedColumns="false" DataSourceID="dsSubsSvc" DataKeyNames="ikCustomer,ikService" OnRowCommand="ActualSvcCmd">
 
<columns>
   <asp:boundfield DataField="sProductCode" HeaderText="Product" />
   <asp:boundfield DataField="sProductDesc" HeaderText="Description" />
   <asp:boundfield DataField="nNormPrice" HeaderText="Normal Price" />
   <asp:boundfield DataField="nDiscPrice" HeaderText="DiscountPrice" />
   <asp:TemplateField>
       <ItemTemplate>
           <asp:Button ID="DoAction1" runat="server" Text="Action1" CommandName="Action1" CommandArgument='<%# Eval("iKey") %>'
 
           <asp:Button ID="DoAction2" runat="server" Text="Action2" CommandName="Action2" CommandArgument='<%# Eval("iKey") %>'
 
           <asp:Button ID="DoAction3" runat="server" Text="Action3" CommandName="Action3" CommandArgument='<%# Eval("iKey") %>'
       </ItemTemplate>
   </asp:TemplateField>
</columns>
</asp:GridView>
 
CODE BEHIND FILE...
Protected Sub ActualSvcCmd(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
   Dim ikService, ikCustomer As Long
 
   ikCustomer = ddlCompany.SelectedItem.Value
   ikService = e.CommandArgument.ToString()   
 
   ' HERE I NEED THE ROW TO BE SELECTED FIRST,
   ' THEN DO ANY ACTION IN THE FOLLOWING SELECT...CASE
   Select Case e.CommandName
        Case "Action1"
        Case "Action2"
        Case "Action3"
   End Select
End Sub
[+][-]04.26.2008 at 08:37AM PDT, ID: 21445847

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.28.2008 at 01:51PM PDT, ID: 21457515

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.28.2008 at 04:08PM PDT, ID: 21458412

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Programming for ASP.NET, .NET, Microsoft Visual Basic.Net
Tags: ASP.NET
Sign Up Now!
Solution Provided By: cdebel
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628